Compare commits

...

3 Commits

4 changed files with 219 additions and 284 deletions

View File

@ -52,16 +52,19 @@
//! 5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) becomes 669060.
//! ((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 becomes 23340.
//! What do you get if you add up the results of evaluating the homework problems using these new rules?
use std::collections::VecDeque;
use aoc_runner_derive::{aoc, aoc_generator};
#[derive(Clone, Copy, Debug, PartialEq)]
enum Token {
pub enum Token {
Num(u64),
Add,
Mul,
Open,
Close,
Space,
Stop,
}
#[aoc_generator(day18)]
@ -83,147 +86,63 @@ fn lex(input: &str) -> Vec<Token> {
})
// Ignore spaces
.filter(|t| t != &Token::Space)
.chain(std::iter::once(Token::Stop))
.collect()
}
fn parse_part1(tokens: &[Token]) -> u64 {
/*
let mut p = Parser::default();
dbg!(&p);
tokens.into_iter().for_each(|t| p.add_token(t));
p.eval()
*/
let mut stack = vec![Vec::new()];
let mut cur_stack = 0;
// Reverse the token list so we can pop them.
let mut tokens: Vec<_> = tokens.into_iter().rev().collect();
while let Some(t) = tokens.pop() {
let mut peek = || {
let t = tokens.pop().unwrap();
tokens.push(t);
t
};
mod part1 {
use std::collections::VecDeque;
use super::Token;
fn parse_item(tokens: &mut VecDeque<Token>) -> u64 {
let t = tokens.pop_front().unwrap();
match t {
Token::Num(n) => {
let t2 = peek();
if let Token::Add = t2 {
stack.push(vec![]);
cur_stack += 1;
}
match stack[cur_stack].last() {
Some(Token::Add) => {
stack[cur_stack].pop();
if let Some(Token::Num(left)) = stack[cur_stack].pop() {
stack[cur_stack].push(Token::Num(left + n));
}
}
Some(Token::Mul) => {
stack[cur_stack].pop();
if let Some(Token::Num(left)) = stack[cur_stack].pop() {
stack[cur_stack].push(Token::Num(left * n));
}
}
None => stack[cur_stack].push(*t),
c => {
dbg!(&c);
}
}
}
Token::Add => stack[cur_stack].push(*t),
Token::Mul => stack[cur_stack].push(*t),
Token::Num(n) => return n,
Token::Open => {
stack.push(vec![]);
cur_stack += 1;
}
Token::Close => {
// Take the result of this parenthetical group and push it on to the stack one
// level below.
assert_eq!(stack[cur_stack].len(), 1);
let t = stack[cur_stack].pop().unwrap();
stack.pop();
cur_stack -= 1;
stack[cur_stack].push(t);
// If the stack has 3 things, it was waiting for this result.
let len = stack[cur_stack].len();
if len >= 3 {
let s = &mut stack[cur_stack];
match (s.pop(), s.pop(), s.pop()) {
(Some(Token::Num(right)), Some(op), Some(Token::Num(left))) => match op {
Token::Add => stack[cur_stack].push(Token::Num(left + right)),
Token::Mul => stack[cur_stack].push(Token::Num(left * right)),
d => panic!(format!("unexpected op {:?}", d)),
},
d => panic!(format!("unexpected trio from on stack: {:?}", d)),
}
let expr = parse_expression(tokens);
if let Token::Close = tokens[0] {
// TODO(wathiede): how to do not?
} else {
panic!("expected close paren");
}
tokens.pop_front();
return expr;
}
Token::Space => unreachable!("no space should be present"),
t => panic!(format!("unexpected token {:?}", t)),
};
}
match stack[cur_stack].last() {
Some(Token::Num(n)) => *n,
d => panic!(format!("Unexpected stack contents: {:?}", d)),
pub fn parse_expression(tokens: &mut VecDeque<Token>) -> u64 {
let mut result = parse_item(tokens);
let mut t = tokens[0];
loop {
match t {
Token::Mul => {
tokens.pop_front();
let rhs = parse_item(tokens);
if let Token::Mul = t {
result = result * rhs;
}
t = tokens[0];
}
Token::Add => {
tokens.pop_front();
let rhs = parse_item(tokens);
if let Token::Add = t {
result = result + rhs;
}
t = tokens[0];
}
_ => break,
}
}
result
}
}
fn parse_part2(tokens: &[Token]) -> u64 {
let mut stack = vec![Vec::new()];
let mut cur_stack = 0;
tokens.iter().for_each(|t| {
match t {
Token::Num(n) => match stack[cur_stack].last() {
Some(Token::Add) => {
stack[cur_stack].pop();
if let Some(Token::Num(left)) = stack[cur_stack].pop() {
stack[cur_stack].push(Token::Num(left + n));
}
}
Some(Token::Mul) => {
stack[cur_stack].pop();
if let Some(Token::Num(left)) = stack[cur_stack].pop() {
stack[cur_stack].push(Token::Num(left * n));
}
}
None => stack[cur_stack].push(*t),
c => {
dbg!(&c);
}
},
Token::Add => stack[cur_stack].push(*t),
Token::Mul => stack[cur_stack].push(*t),
Token::Open => {
stack.push(vec![]);
cur_stack += 1;
}
Token::Close => {
// Take the result of this parenthetical group and push it on to the stack one
// level below.
assert_eq!(stack[cur_stack].len(), 1);
let t = stack[cur_stack].pop().unwrap();
stack.pop();
cur_stack -= 1;
stack[cur_stack].push(t);
// If the stack has 3 things, it was waiting for this result.
let len = stack[cur_stack].len();
if len >= 3 {
let s = &mut stack[cur_stack];
match (s.pop(), s.pop(), s.pop()) {
(Some(Token::Num(right)), Some(op), Some(Token::Num(left))) => match op {
Token::Add => stack[cur_stack].push(Token::Num(left + right)),
Token::Mul => stack[cur_stack].push(Token::Num(left * right)),
d => panic!(format!("unexpected op {:?}", d)),
},
d => panic!(format!("unexpected trio from on stack: {:?}", d)),
}
}
}
Token::Space => unreachable!("no space should be present"),
};
});
match stack[cur_stack].last() {
Some(Token::Num(n)) => *n,
d => panic!(format!("Unexpected stack contents: {:?}", d)),
}
fn parse_part1(tokens: &[Token]) -> u64 {
let mut vd: VecDeque<Token> = tokens.into_iter().cloned().collect();
part1::parse_expression(&mut vd)
}
#[aoc(day18, part1)]
@ -231,6 +150,62 @@ fn solution1(tokens_list: &[Vec<Token>]) -> u64 {
tokens_list.iter().map(|tokens| parse_part1(tokens)).sum()
}
mod part2 {
use std::collections::VecDeque;
use super::Token;
fn parse_item(tokens: &mut VecDeque<Token>) -> u64 {
let t = tokens.pop_front().unwrap();
match t {
Token::Num(n) => return n,
Token::Open => {
let expr = parse_expression(tokens);
if let Token::Close = tokens[0] {
// TODO(wathiede): how to do not?
} else {
panic!("expected close paren");
}
tokens.pop_front();
return expr;
}
t => panic!(format!("unexpected token {:?}", t)),
};
}
fn parse_term(tokens: &mut VecDeque<Token>) -> u64 {
let mut result = parse_item(tokens);
let mut t = tokens[0];
while let Token::Add = t {
tokens.pop_front();
let rhs = parse_item(tokens);
if let Token::Add = t {
result = result + rhs;
}
t = tokens[0];
}
result
}
pub fn parse_expression(tokens: &mut VecDeque<Token>) -> u64 {
let mut result = parse_term(tokens);
let mut t = tokens[0];
while let Token::Mul = t {
tokens.pop_front();
let rhs = parse_term(tokens);
if let Token::Mul = t {
result = result * rhs;
}
t = tokens[0];
}
result
}
}
fn parse_part2(tokens: &[Token]) -> u64 {
let mut vd: VecDeque<Token> = tokens.into_iter().cloned().collect();
part2::parse_expression(&mut vd)
}
#[aoc(day18, part2)]
fn solution2(tokens_list: &[Vec<Token>]) -> u64 {
tokens_list.iter().map(|tokens| parse_part2(tokens)).sum()
@ -252,6 +227,7 @@ mod tests {
Token::Mul,
Token::Num(5),
Token::Close,
Token::Stop,
];
assert_eq!(lex("2 * 3 + (4 * 5)"), tokens);
}

View File

@ -332,7 +332,7 @@ impl Index<(usize, usize)> for Tile {
}
}
#[cfg(debug_assertions)]
#[cfg(any(debug_assertions, test))]
fn border_to_str(border: &[u8]) -> String {
std::str::from_utf8(border).unwrap().to_string()
}
@ -535,7 +535,7 @@ fn stitch(tiles: &[Tile]) -> Tile {
})
});
#[cfg(debug_assertions)]
#[cfg(any(debug_assertions, test))]
border_counts.iter().for_each(|(b, c)| {
let _ = b;
let _ = c;
@ -964,14 +964,6 @@ mod tests {
fn test_stitch() {
let want: Tile = OUTPUT_IMAGE.parse().expect("can't parse stitched input");
let output = stitch(&generator(INPUT));
/*
let output = reorient(&output, |im| {
dbg!(&want, &im);
im == &want
})
.unwrap();
*/
let output = reorient(&output, contains_seamonster);
match output {

View File

@ -85,7 +85,6 @@
//! Determine which two cups will end up immediately clockwise of cup 1. What do you get if you multiply their labels together?
use std::fmt;
use std::ops::{Index, IndexMut, Range, RangeFrom};
use aoc_runner_derive::aoc;
@ -122,38 +121,54 @@ trait Hand {
fn test_cur_to_end(&self) -> Vec<usize>;
}
#[derive(Debug)]
struct TargetCup {
val: usize,
idx: usize,
}
/// TODO(wathiede): redo based on this sentence from glenng:
/// `So a circular linked list containing 2,1,3 would be [3,1,2]`
#[derive(Debug)]
struct FastHand {
idx_to_val: Vec<usize>,
val_to_idx: Vec<usize>,
cur: usize,
// A cup labeled `1` will be represented by the index 0, in that cell will be the index of cup
// clockwise to `1`.
// Stores the next cup as indexed value (i.e. label-1).
cups: Vec<usize>,
cur: Cup,
min: usize,
max: usize,
}
/// Stores the label of a cup. Use `as_idx` to compute the index into FastHand.cups. Use
/// `from_idx` to build a `Cup` from a given index into FastHand.cups.
#[derive(Copy, Clone, Debug, PartialEq)]
struct Cup(usize);
impl Cup {
fn new(val: usize) -> Cup {
Cup(val)
}
fn from_idx(idx: usize) -> Cup {
Cup(idx + 1)
}
fn as_idx(&self) -> usize {
self.0 - 1
}
}
impl FastHand {
fn new(s: &str) -> FastHand {
let data: Vec<_> = s.bytes().map(|s| (s - b'0') as usize).collect();
let min = *data.iter().min().unwrap();
let max = *data.iter().max().unwrap();
let mut idx_to_val = vec![0; data.len()];
let mut val_to_idx = vec![0; data.len()];
data.into_iter().enumerate().for_each(|(idx, val)| {
val_to_idx[val - 1] = idx;
idx_to_val[idx] = val;
let mut cups = vec![0; max];
let mut last = 0;
data.windows(2).for_each(|nums| {
let cur_cup = Cup::new(nums[0]);
let next_cup = Cup::new(nums[1]);
last = next_cup.as_idx();
cups[cur_cup.as_idx()] = next_cup.as_idx();
});
let cur = Cup(data[0]);
cups[last] = cur.as_idx();
FastHand {
idx_to_val,
val_to_idx,
cur: 0,
cups,
cur,
min,
max,
}
@ -164,148 +179,112 @@ impl FastHand {
let mut max = *data.iter().max().unwrap();
data.extend(max + 1..=1000000);
max = 1000000;
let mut idx_to_val = vec![0; data.len()];
let mut val_to_idx = vec![0; data.len()];
data.into_iter().enumerate().for_each(|(idx, val)| {
val_to_idx[val - 1] = idx;
idx_to_val[idx] = val;
let mut cups = vec![0; max];
let mut last = 0;
data.windows(2).for_each(|nums| {
let cur_cup = Cup::new(nums[0]);
let next_cup = Cup::new(nums[1]);
last = next_cup.as_idx();
cups[cur_cup.as_idx()] = next_cup.as_idx();
});
let cur = Cup(data[0]);
cups[last] = cur.as_idx();
FastHand {
idx_to_val,
val_to_idx,
cur: 0,
cups,
cur,
min,
max,
}
}
fn destination_cup_idx(&self, skip_vals: &[usize]) -> usize {
let mut search_val = self.idx_to_val[self.cur] - 1;
fn destination(&self, skip_vals: &[Cup]) -> Cup {
let mut search_val = Cup::new(self.cur.0 - 1);
while skip_vals.contains(&search_val) {
search_val -= 1;
search_val = Cup::new(search_val.0 - 1);
}
if search_val < self.min {
search_val = self.max;
if search_val.0 < self.min {
search_val = Cup::new(self.max);
}
while skip_vals.contains(&search_val) {
search_val -= 1;
search_val = Cup::new(search_val.0 - 1);
}
search_val
}
self.val_to_idx[search_val - 1]
fn next(&self, c: Cup) -> Cup {
//dbg!(c.as_idx(), self.cups[c.as_idx()]);
Cup::from_idx(self.cups[c.as_idx()])
}
}
impl fmt::Display for FastHand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (idx, val) in self.idx_to_val.iter().enumerate() {
if idx == self.cur {
write!(f, "({}) ", val)?;
} else {
write!(f, "{} ", val)?;
};
let mut cur = self.cur;
write!(f, "({}) ", cur.0)?;
for _ in 1..self.cups.len() {
cur = Cup::from_idx(self.cups[cur.as_idx()]);
write!(f, "{} ", cur.0)?;
}
Ok(())
}
}
#[derive(Debug)]
struct CircleVec<T> {
data: Vec<T>,
}
impl<T> CircleVec<T> {
fn len(&self) -> usize {
self.data.len()
}
fn iter(&self) -> impl Iterator<Item = &T> {
self.data.iter()
}
}
// TODO(wathiede): Index<Range> and Index<RangeFrom>?
impl<T> Index<usize> for CircleVec<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
&self.data[index % self.data.len()]
}
}
impl<T> IndexMut<usize> for CircleVec<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
let len = self.data.len();
&mut self.data[index % len]
}
}
impl Hand for FastHand {
fn step(&mut self) {
let n_cups = self.idx_to_val.len();
let right_idx = self.cur + 1;
let mut three = vec![0; 3];
(0..3).for_each(|dst| three[dst] = self.idx_to_val[(right_idx + dst) % n_cups]);
let dst_idx = self.destination_cup_idx(&three);
// TODO
let mut cur = self.cur;
let three: Vec<_> = (0..3)
.map(|_| {
cur = self.next(cur);
cur
})
.collect();
let dst = self.destination(&three);
debug_println!(
"before {} three {:?} target {}",
"cur {} cups {} three {:?} destination {:?}",
self.cur.0,
self,
three,
self.idx_to_val[dst_idx]
dst
);
debug_println!("cups (raw) {:?}", self.cups);
//dbg!(right, &dst);
let end_idx = if dst_idx < right_idx {
n_cups + dst_idx - 3 + 1
} else {
dst_idx + 1 - 3
};
debug_println!("moving window {}.. to {}..{}", dst_idx, right_idx, end_idx);
(right_idx..end_idx)
// Allow wrap around.
.zip((right_idx + 3..).chain(0..))
.for_each(|(dst_idx, src_idx)| {
let src_idx = src_idx % n_cups;
let dst_idx = dst_idx % n_cups;
let v = self.idx_to_val[src_idx];
debug_println!(
"moving {}({}) -> {}({})",
v,
src_idx,
self.idx_to_val[dst_idx],
dst_idx
);
self.idx_to_val[dst_idx] = v;
self.val_to_idx[v - 1] = dst_idx;
});
(0..3).for_each(|i| {
let dst_idx = (end_idx + i) % n_cups;
self.idx_to_val[dst_idx] = three[i];
self.val_to_idx[three[i] - 1] = dst_idx;
});
self.cur = (self.cur + 1) % n_cups;
debug_println!(" after {}", self);
// Cur points to whatever end of three used to.
self.cups[self.cur.as_idx()] = self.cups[three[2].as_idx()];
// End of three points to whatever dst used to point to.
self.cups[three[2].as_idx()] = self.cups[dst.as_idx()];
// Dst points to the beginning of three.
self.cups[dst.as_idx()] = three[0].as_idx();
// Cur points to whatever is next in the circle.
self.cur = self.next(self.cur);
}
fn test_cur_to_end(&self) -> Vec<usize> {
self.idx_to_val[self.cur..]
.iter()
.chain(self.idx_to_val[..self.cur].iter())
.cloned()
.collect()
let mut res = Vec::with_capacity(self.cups.len());
let mut cur = self.cur;
(0..self.cups.len()).for_each(|_| {
res.push(cur.0);
cur = Cup::from_idx(self.cups[cur.as_idx()]);
});
res
}
fn part1_answer(&self) -> String {
let one_idx = self.val_to_idx[1 - 1];
let s = self.idx_to_val[one_idx + 1..]
.iter()
.fold("".to_string(), |acc, c| format!("{}{}", acc, c));
self.idx_to_val[..one_idx]
.iter()
.fold(s, |acc, c| format!("{}{}", acc, c))
let mut cur = Cup::new(1);
let mut s = "".to_string();
for _ in 1..self.cups.len() {
cur = self.next(cur);
s = format!("{}{}", s, cur.0);
}
s
}
fn part2_answer(&self) -> usize {
let one_idx = self.val_to_idx[1 - 1];
self.idx_to_val[one_idx + 1] * self.idx_to_val[one_idx + 2]
let one = Cup::new(1);
let v1 = self.next(one);
let v2 = self.next(v1);
v1.0 * v2.0
}
}
@ -330,6 +309,7 @@ impl fmt::Display for SlowHand {
}
impl SlowHand {
#[allow(dead_code)]
fn new(s: &str) -> SlowHand {
let cups: Vec<_> = s.bytes().map(|s| (s - b'0') as usize).collect();
let min = *cups.iter().min().unwrap();
@ -341,20 +321,6 @@ impl SlowHand {
max,
}
}
fn new_part2(s: &str) -> SlowHand {
let mut cups: Vec<_> = s.bytes().map(|s| (s - b'0') as usize).collect();
let min = *cups.iter().min().unwrap();
let mut max = *cups.iter().max().unwrap();
cups.extend(max + 1..1000000);
max = 1000000;
SlowHand {
cups,
cur: 0,
min,
max,
}
}
}
impl Hand for SlowHand {
@ -421,7 +387,7 @@ impl Hand for SlowHand {
#[aoc(day23, part1)]
fn solution1(input: &str) -> String {
let mut hand = SlowHand::new(input);
let mut hand = FastHand::new(input);
hand.play(100);
hand.part1_answer()
}
@ -429,7 +395,6 @@ fn solution1(input: &str) -> String {
#[aoc(day23, part2)]
fn solution2(input: &str) -> usize {
let mut hand = FastHand::new_part2(input);
//hand.play(1_000);
hand.play(10_000_000);
hand.part2_answer()
}
@ -460,13 +425,13 @@ mod tests {
}
#[test]
fn slow_step() {
let mut hand = SlowHand::new(INPUT);
let hand = SlowHand::new(INPUT);
test_hand(hand);
}
#[test]
fn fast_step() {
let mut hand = FastHand::new(INPUT);
let hand = FastHand::new(INPUT);
test_hand(hand);
}
@ -487,6 +452,8 @@ mod tests {
fn part1() {
assert_eq!(solution1(INPUT), "67384529");
}
// This is too slow in debug mode due to debug_println, build in release to run.
#[cfg(not(debug_assertions))]
#[test]
fn part2() {
assert_eq!(solution2("389125467"), 149245887792);

View File

@ -7,13 +7,13 @@ pub mod day14;
pub mod day15;
pub mod day16;
pub mod day17;
//pub mod day18;
pub mod day18;
pub mod day19;
pub mod day2;
pub mod day20;
pub mod day21;
pub mod day22;
//pub mod day23;
pub mod day23;
pub mod day24;
pub mod day25;
pub mod day3;