Compare commits
No commits in common. "2f36a0b5e8b4660612406a6a22ba4413583b38d6" and "d935de1fb041c992cb2f934f981c4f0212ef8f08" have entirely different histories.
2f36a0b5e8
...
d935de1fb0
@ -52,19 +52,16 @@
|
|||||||
//! 5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) becomes 669060.
|
//! 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.
|
//! ((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?
|
//! 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};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
pub enum Token {
|
enum Token {
|
||||||
Num(u64),
|
Num(u64),
|
||||||
Add,
|
Add,
|
||||||
Mul,
|
Mul,
|
||||||
Open,
|
Open,
|
||||||
Close,
|
Close,
|
||||||
Space,
|
Space,
|
||||||
Stop,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc_generator(day18)]
|
#[aoc_generator(day18)]
|
||||||
@ -86,63 +83,147 @@ fn lex(input: &str) -> Vec<Token> {
|
|||||||
})
|
})
|
||||||
// Ignore spaces
|
// Ignore spaces
|
||||||
.filter(|t| t != &Token::Space)
|
.filter(|t| t != &Token::Space)
|
||||||
.chain(std::iter::once(Token::Stop))
|
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
mod part1 {
|
fn parse_part1(tokens: &[Token]) -> u64 {
|
||||||
use std::collections::VecDeque;
|
/*
|
||||||
|
let mut p = Parser::default();
|
||||||
use super::Token;
|
dbg!(&p);
|
||||||
fn parse_item(tokens: &mut VecDeque<Token>) -> u64 {
|
tokens.into_iter().for_each(|t| p.add_token(t));
|
||||||
let t = tokens.pop_front().unwrap();
|
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
|
||||||
|
};
|
||||||
match t {
|
match t {
|
||||||
Token::Num(n) => return n,
|
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::Open => {
|
Token::Open => {
|
||||||
let expr = parse_expression(tokens);
|
stack.push(vec![]);
|
||||||
if let Token::Close = tokens[0] {
|
cur_stack += 1;
|
||||||
// TODO(wathiede): how to do not?
|
|
||||||
} else {
|
|
||||||
panic!("expected close paren");
|
|
||||||
}
|
}
|
||||||
tokens.pop_front();
|
Token::Close => {
|
||||||
return expr;
|
// 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)),
|
||||||
}
|
}
|
||||||
t => panic!(format!("unexpected token {:?}", t)),
|
}
|
||||||
|
}
|
||||||
|
Token::Space => unreachable!("no space should be present"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
match stack[cur_stack].last() {
|
||||||
pub fn parse_expression(tokens: &mut VecDeque<Token>) -> u64 {
|
Some(Token::Num(n)) => *n,
|
||||||
let mut result = parse_item(tokens);
|
d => panic!(format!("Unexpected stack contents: {:?}", d)),
|
||||||
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_part1(tokens: &[Token]) -> u64 {
|
fn parse_part2(tokens: &[Token]) -> u64 {
|
||||||
let mut vd: VecDeque<Token> = tokens.into_iter().cloned().collect();
|
let mut stack = vec![Vec::new()];
|
||||||
part1::parse_expression(&mut vd)
|
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)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day18, part1)]
|
#[aoc(day18, part1)]
|
||||||
@ -150,62 +231,6 @@ fn solution1(tokens_list: &[Vec<Token>]) -> u64 {
|
|||||||
tokens_list.iter().map(|tokens| parse_part1(tokens)).sum()
|
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)]
|
#[aoc(day18, part2)]
|
||||||
fn solution2(tokens_list: &[Vec<Token>]) -> u64 {
|
fn solution2(tokens_list: &[Vec<Token>]) -> u64 {
|
||||||
tokens_list.iter().map(|tokens| parse_part2(tokens)).sum()
|
tokens_list.iter().map(|tokens| parse_part2(tokens)).sum()
|
||||||
@ -227,7 +252,6 @@ mod tests {
|
|||||||
Token::Mul,
|
Token::Mul,
|
||||||
Token::Num(5),
|
Token::Num(5),
|
||||||
Token::Close,
|
Token::Close,
|
||||||
Token::Stop,
|
|
||||||
];
|
];
|
||||||
assert_eq!(lex("2 * 3 + (4 * 5)"), tokens);
|
assert_eq!(lex("2 * 3 + (4 * 5)"), tokens);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -332,7 +332,7 @@ impl Index<(usize, usize)> for Tile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(debug_assertions, test))]
|
#[cfg(debug_assertions)]
|
||||||
fn border_to_str(border: &[u8]) -> String {
|
fn border_to_str(border: &[u8]) -> String {
|
||||||
std::str::from_utf8(border).unwrap().to_string()
|
std::str::from_utf8(border).unwrap().to_string()
|
||||||
}
|
}
|
||||||
@ -535,7 +535,7 @@ fn stitch(tiles: &[Tile]) -> Tile {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
#[cfg(any(debug_assertions, test))]
|
#[cfg(debug_assertions)]
|
||||||
border_counts.iter().for_each(|(b, c)| {
|
border_counts.iter().for_each(|(b, c)| {
|
||||||
let _ = b;
|
let _ = b;
|
||||||
let _ = c;
|
let _ = c;
|
||||||
@ -964,6 +964,14 @@ mod tests {
|
|||||||
fn test_stitch() {
|
fn test_stitch() {
|
||||||
let want: Tile = OUTPUT_IMAGE.parse().expect("can't parse stitched input");
|
let want: Tile = OUTPUT_IMAGE.parse().expect("can't parse stitched input");
|
||||||
let output = stitch(&generator(INPUT));
|
let output = stitch(&generator(INPUT));
|
||||||
|
|
||||||
|
/*
|
||||||
|
let output = reorient(&output, |im| {
|
||||||
|
dbg!(&want, &im);
|
||||||
|
im == &want
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
*/
|
||||||
let output = reorient(&output, contains_seamonster);
|
let output = reorient(&output, contains_seamonster);
|
||||||
|
|
||||||
match output {
|
match output {
|
||||||
|
|||||||
@ -85,6 +85,7 @@
|
|||||||
//! Determine which two cups will end up immediately clockwise of cup 1. What do you get if you multiply their labels together?
|
//! 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::fmt;
|
||||||
|
use std::ops::{Index, IndexMut, Range, RangeFrom};
|
||||||
|
|
||||||
use aoc_runner_derive::aoc;
|
use aoc_runner_derive::aoc;
|
||||||
|
|
||||||
@ -121,54 +122,38 @@ trait Hand {
|
|||||||
fn test_cur_to_end(&self) -> Vec<usize>;
|
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:
|
/// TODO(wathiede): redo based on this sentence from glenng:
|
||||||
/// `So a circular linked list containing 2,1,3 would be [3,1,2]`
|
/// `So a circular linked list containing 2,1,3 would be [3,1,2]`
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct FastHand {
|
struct FastHand {
|
||||||
// A cup labeled `1` will be represented by the index 0, in that cell will be the index of cup
|
idx_to_val: Vec<usize>,
|
||||||
// clockwise to `1`.
|
val_to_idx: Vec<usize>,
|
||||||
// Stores the next cup as indexed value (i.e. label-1).
|
cur: usize,
|
||||||
cups: Vec<usize>,
|
|
||||||
cur: Cup,
|
|
||||||
min: usize,
|
min: usize,
|
||||||
max: 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 {
|
impl FastHand {
|
||||||
fn new(s: &str) -> FastHand {
|
fn new(s: &str) -> FastHand {
|
||||||
let data: Vec<_> = s.bytes().map(|s| (s - b'0') as usize).collect();
|
let data: Vec<_> = s.bytes().map(|s| (s - b'0') as usize).collect();
|
||||||
let min = *data.iter().min().unwrap();
|
let min = *data.iter().min().unwrap();
|
||||||
let max = *data.iter().max().unwrap();
|
let max = *data.iter().max().unwrap();
|
||||||
let mut cups = vec![0; max];
|
let mut idx_to_val = vec![0; data.len()];
|
||||||
let mut last = 0;
|
let mut val_to_idx = vec![0; data.len()];
|
||||||
data.windows(2).for_each(|nums| {
|
data.into_iter().enumerate().for_each(|(idx, val)| {
|
||||||
let cur_cup = Cup::new(nums[0]);
|
val_to_idx[val - 1] = idx;
|
||||||
let next_cup = Cup::new(nums[1]);
|
idx_to_val[idx] = val;
|
||||||
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 {
|
FastHand {
|
||||||
cups,
|
idx_to_val,
|
||||||
cur,
|
val_to_idx,
|
||||||
|
cur: 0,
|
||||||
min,
|
min,
|
||||||
max,
|
max,
|
||||||
}
|
}
|
||||||
@ -179,112 +164,148 @@ impl FastHand {
|
|||||||
let mut max = *data.iter().max().unwrap();
|
let mut max = *data.iter().max().unwrap();
|
||||||
data.extend(max + 1..=1000000);
|
data.extend(max + 1..=1000000);
|
||||||
max = 1000000;
|
max = 1000000;
|
||||||
let mut cups = vec![0; max];
|
let mut idx_to_val = vec![0; data.len()];
|
||||||
let mut last = 0;
|
let mut val_to_idx = vec![0; data.len()];
|
||||||
data.windows(2).for_each(|nums| {
|
data.into_iter().enumerate().for_each(|(idx, val)| {
|
||||||
let cur_cup = Cup::new(nums[0]);
|
val_to_idx[val - 1] = idx;
|
||||||
let next_cup = Cup::new(nums[1]);
|
idx_to_val[idx] = val;
|
||||||
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 {
|
FastHand {
|
||||||
cups,
|
idx_to_val,
|
||||||
cur,
|
val_to_idx,
|
||||||
|
cur: 0,
|
||||||
min,
|
min,
|
||||||
max,
|
max,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn destination(&self, skip_vals: &[Cup]) -> Cup {
|
fn destination_cup_idx(&self, skip_vals: &[usize]) -> usize {
|
||||||
let mut search_val = Cup::new(self.cur.0 - 1);
|
let mut search_val = self.idx_to_val[self.cur] - 1;
|
||||||
while skip_vals.contains(&search_val) {
|
while skip_vals.contains(&search_val) {
|
||||||
search_val = Cup::new(search_val.0 - 1);
|
search_val -= 1;
|
||||||
}
|
|
||||||
if search_val.0 < self.min {
|
|
||||||
search_val = Cup::new(self.max);
|
|
||||||
}
|
|
||||||
while skip_vals.contains(&search_val) {
|
|
||||||
search_val = Cup::new(search_val.0 - 1);
|
|
||||||
}
|
|
||||||
search_val
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next(&self, c: Cup) -> Cup {
|
if search_val < self.min {
|
||||||
//dbg!(c.as_idx(), self.cups[c.as_idx()]);
|
search_val = self.max;
|
||||||
Cup::from_idx(self.cups[c.as_idx()])
|
}
|
||||||
|
while skip_vals.contains(&search_val) {
|
||||||
|
search_val -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.val_to_idx[search_val - 1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for FastHand {
|
impl fmt::Display for FastHand {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let mut cur = self.cur;
|
for (idx, val) in self.idx_to_val.iter().enumerate() {
|
||||||
write!(f, "({}) ", cur.0)?;
|
if idx == self.cur {
|
||||||
|
write!(f, "({}) ", val)?;
|
||||||
for _ in 1..self.cups.len() {
|
} else {
|
||||||
cur = Cup::from_idx(self.cups[cur.as_idx()]);
|
write!(f, "{} ", val)?;
|
||||||
write!(f, "{} ", cur.0)?;
|
};
|
||||||
}
|
}
|
||||||
Ok(())
|
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 {
|
impl Hand for FastHand {
|
||||||
fn step(&mut self) {
|
fn step(&mut self) {
|
||||||
let mut cur = self.cur;
|
let n_cups = self.idx_to_val.len();
|
||||||
let three: Vec<_> = (0..3)
|
let right_idx = self.cur + 1;
|
||||||
.map(|_| {
|
let mut three = vec![0; 3];
|
||||||
cur = self.next(cur);
|
(0..3).for_each(|dst| three[dst] = self.idx_to_val[(right_idx + dst) % n_cups]);
|
||||||
cur
|
let dst_idx = self.destination_cup_idx(&three);
|
||||||
})
|
|
||||||
.collect();
|
// TODO
|
||||||
let dst = self.destination(&three);
|
|
||||||
debug_println!(
|
debug_println!(
|
||||||
"cur {} cups {} three {:?} destination {:?}",
|
"before {} three {:?} target {}",
|
||||||
self.cur.0,
|
|
||||||
self,
|
self,
|
||||||
three,
|
three,
|
||||||
dst
|
self.idx_to_val[dst_idx]
|
||||||
);
|
);
|
||||||
debug_println!("cups (raw) {:?}", self.cups);
|
|
||||||
|
|
||||||
// Cur points to whatever end of three used to.
|
//dbg!(right, &dst);
|
||||||
self.cups[self.cur.as_idx()] = self.cups[three[2].as_idx()];
|
let end_idx = if dst_idx < right_idx {
|
||||||
|
n_cups + dst_idx - 3 + 1
|
||||||
// End of three points to whatever dst used to point to.
|
} else {
|
||||||
self.cups[three[2].as_idx()] = self.cups[dst.as_idx()];
|
dst_idx + 1 - 3
|
||||||
|
};
|
||||||
// Dst points to the beginning of three.
|
debug_println!("moving window {}.. to {}..{}", dst_idx, right_idx, end_idx);
|
||||||
self.cups[dst.as_idx()] = three[0].as_idx();
|
(right_idx..end_idx)
|
||||||
|
// Allow wrap around.
|
||||||
// Cur points to whatever is next in the circle.
|
.zip((right_idx + 3..).chain(0..))
|
||||||
self.cur = self.next(self.cur);
|
.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);
|
||||||
}
|
}
|
||||||
fn test_cur_to_end(&self) -> Vec<usize> {
|
fn test_cur_to_end(&self) -> Vec<usize> {
|
||||||
let mut res = Vec::with_capacity(self.cups.len());
|
self.idx_to_val[self.cur..]
|
||||||
let mut cur = self.cur;
|
.iter()
|
||||||
(0..self.cups.len()).for_each(|_| {
|
.chain(self.idx_to_val[..self.cur].iter())
|
||||||
res.push(cur.0);
|
.cloned()
|
||||||
cur = Cup::from_idx(self.cups[cur.as_idx()]);
|
.collect()
|
||||||
});
|
|
||||||
res
|
|
||||||
}
|
}
|
||||||
fn part1_answer(&self) -> String {
|
fn part1_answer(&self) -> String {
|
||||||
let mut cur = Cup::new(1);
|
let one_idx = self.val_to_idx[1 - 1];
|
||||||
let mut s = "".to_string();
|
let s = self.idx_to_val[one_idx + 1..]
|
||||||
for _ in 1..self.cups.len() {
|
.iter()
|
||||||
cur = self.next(cur);
|
.fold("".to_string(), |acc, c| format!("{}{}", acc, c));
|
||||||
s = format!("{}{}", s, cur.0);
|
self.idx_to_val[..one_idx]
|
||||||
}
|
.iter()
|
||||||
|
.fold(s, |acc, c| format!("{}{}", acc, c))
|
||||||
s
|
|
||||||
}
|
}
|
||||||
fn part2_answer(&self) -> usize {
|
fn part2_answer(&self) -> usize {
|
||||||
let one = Cup::new(1);
|
let one_idx = self.val_to_idx[1 - 1];
|
||||||
let v1 = self.next(one);
|
self.idx_to_val[one_idx + 1] * self.idx_to_val[one_idx + 2]
|
||||||
let v2 = self.next(v1);
|
|
||||||
v1.0 * v2.0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -309,7 +330,6 @@ impl fmt::Display for SlowHand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SlowHand {
|
impl SlowHand {
|
||||||
#[allow(dead_code)]
|
|
||||||
fn new(s: &str) -> SlowHand {
|
fn new(s: &str) -> SlowHand {
|
||||||
let cups: Vec<_> = s.bytes().map(|s| (s - b'0') as usize).collect();
|
let cups: Vec<_> = s.bytes().map(|s| (s - b'0') as usize).collect();
|
||||||
let min = *cups.iter().min().unwrap();
|
let min = *cups.iter().min().unwrap();
|
||||||
@ -321,6 +341,20 @@ impl SlowHand {
|
|||||||
max,
|
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 {
|
impl Hand for SlowHand {
|
||||||
@ -387,7 +421,7 @@ impl Hand for SlowHand {
|
|||||||
|
|
||||||
#[aoc(day23, part1)]
|
#[aoc(day23, part1)]
|
||||||
fn solution1(input: &str) -> String {
|
fn solution1(input: &str) -> String {
|
||||||
let mut hand = FastHand::new(input);
|
let mut hand = SlowHand::new(input);
|
||||||
hand.play(100);
|
hand.play(100);
|
||||||
hand.part1_answer()
|
hand.part1_answer()
|
||||||
}
|
}
|
||||||
@ -395,6 +429,7 @@ fn solution1(input: &str) -> String {
|
|||||||
#[aoc(day23, part2)]
|
#[aoc(day23, part2)]
|
||||||
fn solution2(input: &str) -> usize {
|
fn solution2(input: &str) -> usize {
|
||||||
let mut hand = FastHand::new_part2(input);
|
let mut hand = FastHand::new_part2(input);
|
||||||
|
//hand.play(1_000);
|
||||||
hand.play(10_000_000);
|
hand.play(10_000_000);
|
||||||
hand.part2_answer()
|
hand.part2_answer()
|
||||||
}
|
}
|
||||||
@ -425,13 +460,13 @@ mod tests {
|
|||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn slow_step() {
|
fn slow_step() {
|
||||||
let hand = SlowHand::new(INPUT);
|
let mut hand = SlowHand::new(INPUT);
|
||||||
test_hand(hand);
|
test_hand(hand);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn fast_step() {
|
fn fast_step() {
|
||||||
let hand = FastHand::new(INPUT);
|
let mut hand = FastHand::new(INPUT);
|
||||||
test_hand(hand);
|
test_hand(hand);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -452,8 +487,6 @@ mod tests {
|
|||||||
fn part1() {
|
fn part1() {
|
||||||
assert_eq!(solution1(INPUT), "67384529");
|
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]
|
#[test]
|
||||||
fn part2() {
|
fn part2() {
|
||||||
assert_eq!(solution2("389125467"), 149245887792);
|
assert_eq!(solution2("389125467"), 149245887792);
|
||||||
|
|||||||
@ -7,13 +7,13 @@ pub mod day14;
|
|||||||
pub mod day15;
|
pub mod day15;
|
||||||
pub mod day16;
|
pub mod day16;
|
||||||
pub mod day17;
|
pub mod day17;
|
||||||
pub mod day18;
|
//pub mod day18;
|
||||||
pub mod day19;
|
pub mod day19;
|
||||||
pub mod day2;
|
pub mod day2;
|
||||||
pub mod day20;
|
pub mod day20;
|
||||||
pub mod day21;
|
pub mod day21;
|
||||||
pub mod day22;
|
pub mod day22;
|
||||||
pub mod day23;
|
//pub mod day23;
|
||||||
pub mod day24;
|
pub mod day24;
|
||||||
pub mod day25;
|
pub mod day25;
|
||||||
pub mod day3;
|
pub mod day3;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user