From 38a024d095565f139277b3ab95a4f7cab0134b41 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Mon, 5 Dec 2022 11:08:54 +0100 Subject: [PATCH] Implement 2022 day 5 --- 2022/benches/days.rs | 2 +- 2022/inputs/05.txt | 514 ++++++++++++++++++++++++++++++++++++++++ 2022/src/day05.rs | 168 ++++++++++++- 2022/src/samples/05.txt | 9 + 4 files changed, 688 insertions(+), 5 deletions(-) create mode 100644 2022/inputs/05.txt create mode 100644 2022/src/samples/05.txt diff --git a/2022/benches/days.rs b/2022/benches/days.rs index 814e95e..77e9853 100644 --- a/2022/benches/days.rs +++ b/2022/benches/days.rs @@ -8,7 +8,7 @@ use criterion::BenchmarkId; use criterion::Criterion; /// Number of days we have an implementation to benchmark -const DAYS_IMPLEMENTED: u8 = 4; +const DAYS_IMPLEMENTED: u8 = 5; fn read_input(day: u8) -> Vec { let input_path = format!("inputs/{:02}.txt", day); diff --git a/2022/inputs/05.txt b/2022/inputs/05.txt new file mode 100644 index 0000000..8d7a981 --- /dev/null +++ b/2022/inputs/05.txt @@ -0,0 +1,514 @@ + [G] [D] [Q] +[P] [T] [L] [M] [Z] +[Z] [Z] [C] [Z] [G] [W] +[M] [B] [F] [P] [C] [H] [N] +[T] [S] [R] [H] [W] [R] [L] [W] +[R] [T] [Q] [Z] [R] [S] [Z] [F] [P] +[C] [N] [H] [R] [N] [H] [D] [J] [Q] +[N] [D] [M] [G] [Z] [F] [W] [S] [S] + 1 2 3 4 5 6 7 8 9 + +move 7 from 6 to 8 +move 5 from 2 to 6 +move 2 from 4 to 1 +move 1 from 4 to 5 +move 5 from 7 to 6 +move 7 from 6 to 3 +move 5 from 9 to 2 +move 6 from 2 to 3 +move 2 from 7 to 9 +move 20 from 3 to 1 +move 11 from 1 to 6 +move 1 from 9 to 8 +move 3 from 8 to 2 +move 8 from 1 to 5 +move 10 from 8 to 4 +move 7 from 6 to 4 +move 1 from 8 to 3 +move 8 from 1 to 7 +move 16 from 4 to 8 +move 1 from 9 to 8 +move 1 from 5 to 2 +move 4 from 7 to 4 +move 5 from 6 to 7 +move 1 from 6 to 1 +move 8 from 7 to 4 +move 1 from 6 to 9 +move 12 from 4 to 5 +move 3 from 2 to 5 +move 1 from 6 to 2 +move 1 from 3 to 7 +move 1 from 3 to 2 +move 1 from 9 to 3 +move 1 from 7 to 8 +move 1 from 7 to 5 +move 1 from 3 to 2 +move 4 from 5 to 7 +move 5 from 5 to 7 +move 1 from 4 to 3 +move 1 from 3 to 9 +move 3 from 1 to 8 +move 1 from 9 to 1 +move 2 from 2 to 1 +move 2 from 2 to 7 +move 8 from 8 to 1 +move 3 from 5 to 2 +move 8 from 7 to 5 +move 7 from 1 to 3 +move 3 from 1 to 7 +move 1 from 1 to 5 +move 1 from 3 to 7 +move 7 from 5 to 8 +move 2 from 2 to 8 +move 1 from 3 to 2 +move 1 from 2 to 4 +move 1 from 4 to 8 +move 13 from 8 to 1 +move 13 from 5 to 9 +move 2 from 5 to 2 +move 7 from 9 to 3 +move 12 from 8 to 3 +move 4 from 9 to 3 +move 1 from 3 to 4 +move 2 from 2 to 3 +move 1 from 1 to 6 +move 1 from 2 to 3 +move 1 from 5 to 9 +move 7 from 7 to 4 +move 10 from 1 to 8 +move 1 from 1 to 4 +move 1 from 9 to 5 +move 2 from 5 to 1 +move 1 from 6 to 5 +move 3 from 8 to 9 +move 5 from 4 to 3 +move 4 from 4 to 1 +move 7 from 1 to 6 +move 2 from 5 to 7 +move 35 from 3 to 4 +move 4 from 9 to 1 +move 19 from 4 to 8 +move 1 from 7 to 6 +move 1 from 9 to 2 +move 10 from 4 to 5 +move 2 from 4 to 7 +move 3 from 4 to 3 +move 1 from 2 to 8 +move 1 from 1 to 9 +move 3 from 3 to 6 +move 4 from 8 to 6 +move 4 from 5 to 2 +move 2 from 8 to 3 +move 3 from 5 to 9 +move 12 from 6 to 1 +move 8 from 8 to 6 +move 2 from 9 to 1 +move 1 from 4 to 1 +move 1 from 3 to 8 +move 3 from 7 to 8 +move 2 from 9 to 7 +move 1 from 6 to 7 +move 10 from 6 to 8 +move 4 from 2 to 5 +move 1 from 3 to 7 +move 7 from 5 to 7 +move 13 from 8 to 1 +move 29 from 1 to 4 +move 8 from 7 to 8 +move 1 from 1 to 3 +move 3 from 7 to 6 +move 1 from 1 to 9 +move 15 from 4 to 1 +move 1 from 3 to 6 +move 10 from 1 to 6 +move 10 from 6 to 7 +move 1 from 4 to 9 +move 1 from 9 to 1 +move 1 from 9 to 7 +move 6 from 7 to 8 +move 1 from 1 to 6 +move 5 from 6 to 5 +move 21 from 8 to 9 +move 5 from 1 to 9 +move 2 from 9 to 5 +move 3 from 5 to 6 +move 3 from 7 to 9 +move 4 from 4 to 6 +move 6 from 8 to 7 +move 6 from 6 to 3 +move 2 from 7 to 9 +move 1 from 7 to 2 +move 6 from 3 to 2 +move 1 from 6 to 4 +move 4 from 5 to 9 +move 1 from 4 to 5 +move 9 from 4 to 6 +move 7 from 6 to 4 +move 10 from 9 to 2 +move 5 from 7 to 5 +move 10 from 2 to 7 +move 2 from 5 to 4 +move 2 from 5 to 9 +move 4 from 9 to 4 +move 1 from 8 to 6 +move 7 from 7 to 2 +move 1 from 5 to 4 +move 2 from 7 to 1 +move 1 from 5 to 7 +move 3 from 6 to 2 +move 4 from 4 to 5 +move 1 from 2 to 7 +move 10 from 4 to 7 +move 3 from 7 to 3 +move 17 from 9 to 4 +move 1 from 1 to 4 +move 1 from 1 to 5 +move 5 from 2 to 7 +move 1 from 9 to 2 +move 5 from 4 to 8 +move 2 from 9 to 7 +move 4 from 8 to 1 +move 3 from 4 to 8 +move 1 from 2 to 5 +move 1 from 9 to 2 +move 6 from 4 to 8 +move 3 from 7 to 5 +move 1 from 4 to 9 +move 1 from 9 to 1 +move 3 from 1 to 9 +move 4 from 8 to 5 +move 2 from 9 to 8 +move 4 from 2 to 5 +move 8 from 7 to 2 +move 5 from 8 to 5 +move 2 from 7 to 8 +move 1 from 3 to 5 +move 1 from 1 to 2 +move 1 from 1 to 6 +move 2 from 3 to 6 +move 5 from 2 to 8 +move 4 from 7 to 1 +move 7 from 8 to 5 +move 1 from 1 to 5 +move 3 from 8 to 3 +move 1 from 9 to 3 +move 7 from 2 to 3 +move 2 from 2 to 8 +move 2 from 4 to 8 +move 1 from 8 to 5 +move 1 from 1 to 4 +move 2 from 4 to 7 +move 2 from 7 to 1 +move 3 from 2 to 3 +move 3 from 5 to 2 +move 1 from 8 to 3 +move 3 from 3 to 2 +move 5 from 2 to 1 +move 17 from 5 to 8 +move 9 from 8 to 1 +move 11 from 3 to 5 +move 8 from 8 to 5 +move 2 from 8 to 5 +move 16 from 1 to 4 +move 13 from 4 to 7 +move 6 from 5 to 2 +move 2 from 4 to 8 +move 5 from 7 to 9 +move 2 from 1 to 2 +move 7 from 7 to 1 +move 1 from 1 to 4 +move 1 from 9 to 8 +move 7 from 2 to 8 +move 1 from 4 to 7 +move 2 from 9 to 4 +move 1 from 4 to 1 +move 1 from 3 to 5 +move 2 from 9 to 8 +move 11 from 8 to 7 +move 2 from 6 to 5 +move 1 from 6 to 9 +move 1 from 1 to 9 +move 1 from 9 to 1 +move 4 from 1 to 4 +move 2 from 1 to 8 +move 1 from 1 to 2 +move 1 from 9 to 5 +move 2 from 4 to 3 +move 2 from 2 to 7 +move 2 from 3 to 9 +move 1 from 9 to 1 +move 1 from 9 to 1 +move 5 from 5 to 1 +move 19 from 5 to 6 +move 5 from 1 to 4 +move 1 from 2 to 9 +move 1 from 1 to 3 +move 7 from 5 to 8 +move 1 from 3 to 6 +move 8 from 7 to 3 +move 7 from 4 to 8 +move 3 from 8 to 5 +move 1 from 4 to 1 +move 1 from 9 to 4 +move 1 from 4 to 9 +move 1 from 5 to 2 +move 2 from 5 to 6 +move 2 from 8 to 2 +move 7 from 8 to 1 +move 1 from 1 to 7 +move 3 from 6 to 9 +move 2 from 3 to 2 +move 1 from 2 to 1 +move 1 from 8 to 7 +move 2 from 9 to 6 +move 2 from 9 to 5 +move 1 from 5 to 6 +move 1 from 2 to 8 +move 2 from 1 to 7 +move 1 from 4 to 3 +move 3 from 2 to 5 +move 7 from 1 to 3 +move 10 from 3 to 4 +move 3 from 5 to 4 +move 1 from 3 to 8 +move 3 from 3 to 2 +move 1 from 8 to 1 +move 1 from 1 to 3 +move 3 from 8 to 3 +move 5 from 4 to 6 +move 1 from 2 to 3 +move 4 from 6 to 4 +move 1 from 5 to 7 +move 4 from 3 to 4 +move 1 from 2 to 8 +move 12 from 7 to 6 +move 1 from 8 to 2 +move 2 from 2 to 7 +move 1 from 8 to 4 +move 23 from 6 to 3 +move 14 from 3 to 6 +move 15 from 4 to 6 +move 1 from 8 to 6 +move 10 from 3 to 7 +move 2 from 4 to 2 +move 11 from 7 to 8 +move 2 from 2 to 6 +move 44 from 6 to 9 +move 21 from 9 to 3 +move 12 from 3 to 6 +move 1 from 7 to 4 +move 1 from 4 to 7 +move 9 from 3 to 2 +move 2 from 8 to 6 +move 3 from 2 to 4 +move 17 from 9 to 1 +move 3 from 4 to 6 +move 2 from 2 to 9 +move 4 from 9 to 2 +move 10 from 6 to 9 +move 1 from 7 to 6 +move 4 from 9 to 5 +move 4 from 2 to 4 +move 14 from 1 to 5 +move 4 from 4 to 3 +move 3 from 2 to 9 +move 9 from 9 to 7 +move 1 from 2 to 5 +move 9 from 8 to 5 +move 8 from 7 to 2 +move 4 from 3 to 8 +move 5 from 6 to 2 +move 3 from 1 to 6 +move 1 from 7 to 1 +move 4 from 2 to 4 +move 3 from 6 to 4 +move 3 from 8 to 3 +move 13 from 5 to 2 +move 2 from 3 to 5 +move 12 from 5 to 9 +move 1 from 3 to 5 +move 1 from 5 to 9 +move 1 from 8 to 3 +move 4 from 9 to 5 +move 6 from 4 to 5 +move 12 from 9 to 7 +move 1 from 9 to 3 +move 1 from 3 to 2 +move 12 from 5 to 6 +move 12 from 7 to 2 +move 1 from 3 to 7 +move 1 from 4 to 8 +move 33 from 2 to 8 +move 1 from 7 to 5 +move 1 from 1 to 2 +move 4 from 5 to 4 +move 3 from 2 to 5 +move 34 from 8 to 6 +move 1 from 4 to 3 +move 1 from 5 to 7 +move 1 from 7 to 5 +move 3 from 4 to 9 +move 2 from 9 to 7 +move 1 from 9 to 4 +move 1 from 3 to 7 +move 1 from 5 to 8 +move 1 from 5 to 1 +move 1 from 5 to 7 +move 1 from 4 to 8 +move 1 from 1 to 4 +move 1 from 4 to 2 +move 3 from 7 to 5 +move 2 from 8 to 5 +move 1 from 2 to 8 +move 4 from 6 to 2 +move 1 from 8 to 6 +move 1 from 7 to 9 +move 29 from 6 to 7 +move 4 from 2 to 3 +move 2 from 5 to 8 +move 1 from 9 to 5 +move 2 from 8 to 1 +move 23 from 7 to 5 +move 2 from 6 to 1 +move 23 from 5 to 6 +move 1 from 3 to 6 +move 4 from 5 to 9 +move 2 from 1 to 3 +move 5 from 3 to 8 +move 2 from 6 to 5 +move 2 from 1 to 4 +move 1 from 9 to 8 +move 1 from 9 to 1 +move 1 from 4 to 6 +move 2 from 5 to 6 +move 6 from 7 to 8 +move 2 from 9 to 2 +move 18 from 6 to 5 +move 21 from 6 to 4 +move 1 from 1 to 6 +move 2 from 6 to 7 +move 2 from 7 to 9 +move 2 from 2 to 8 +move 7 from 4 to 3 +move 12 from 5 to 3 +move 1 from 9 to 5 +move 1 from 9 to 4 +move 6 from 5 to 2 +move 17 from 3 to 4 +move 3 from 4 to 3 +move 1 from 2 to 4 +move 5 from 2 to 8 +move 1 from 5 to 8 +move 19 from 8 to 7 +move 1 from 3 to 6 +move 1 from 8 to 4 +move 1 from 6 to 1 +move 15 from 4 to 6 +move 1 from 1 to 4 +move 3 from 3 to 5 +move 4 from 6 to 7 +move 1 from 4 to 7 +move 10 from 6 to 7 +move 16 from 4 to 5 +move 24 from 7 to 2 +move 8 from 7 to 8 +move 1 from 4 to 2 +move 6 from 8 to 7 +move 1 from 8 to 7 +move 1 from 6 to 9 +move 14 from 5 to 4 +move 9 from 7 to 8 +move 4 from 5 to 1 +move 2 from 1 to 5 +move 3 from 8 to 6 +move 2 from 6 to 9 +move 2 from 2 to 8 +move 6 from 2 to 7 +move 3 from 4 to 6 +move 1 from 3 to 4 +move 3 from 5 to 7 +move 1 from 6 to 9 +move 5 from 7 to 2 +move 4 from 9 to 1 +move 1 from 7 to 9 +move 9 from 8 to 4 +move 5 from 1 to 2 +move 2 from 6 to 1 +move 6 from 4 to 7 +move 1 from 7 to 3 +move 1 from 3 to 9 +move 1 from 9 to 7 +move 1 from 6 to 7 +move 9 from 4 to 5 +move 7 from 7 to 9 +move 3 from 7 to 5 +move 1 from 9 to 2 +move 6 from 9 to 8 +move 4 from 4 to 5 +move 1 from 4 to 2 +move 1 from 4 to 2 +move 2 from 1 to 2 +move 1 from 9 to 8 +move 10 from 2 to 4 +move 8 from 2 to 7 +move 12 from 2 to 9 +move 6 from 7 to 4 +move 1 from 1 to 2 +move 8 from 9 to 8 +move 7 from 5 to 1 +move 9 from 4 to 3 +move 14 from 8 to 4 +move 1 from 8 to 4 +move 1 from 1 to 5 +move 1 from 5 to 2 +move 3 from 2 to 4 +move 1 from 7 to 1 +move 1 from 7 to 3 +move 2 from 1 to 7 +move 3 from 5 to 7 +move 2 from 7 to 6 +move 1 from 6 to 5 +move 3 from 7 to 1 +move 1 from 6 to 8 +move 1 from 8 to 7 +move 1 from 3 to 6 +move 1 from 7 to 1 +move 4 from 1 to 4 +move 6 from 3 to 2 +move 3 from 1 to 2 +move 3 from 3 to 6 +move 3 from 2 to 6 +move 6 from 6 to 5 +move 1 from 1 to 4 +move 1 from 9 to 6 +move 5 from 2 to 1 +move 3 from 1 to 2 +move 2 from 9 to 8 +move 3 from 1 to 5 +move 1 from 9 to 7 +move 25 from 4 to 1 +move 1 from 1 to 7 +move 2 from 8 to 3 +move 13 from 1 to 9 +move 2 from 3 to 5 +move 8 from 5 to 9 +move 4 from 2 to 1 +move 2 from 6 to 7 +move 10 from 5 to 9 +move 4 from 7 to 2 +move 2 from 2 to 3 +move 9 from 9 to 2 +move 4 from 4 to 5 +move 4 from 5 to 4 +move 5 from 1 to 4 +move 10 from 4 to 5 +move 22 from 9 to 1 +move 2 from 2 to 7 +move 3 from 2 to 1 +move 6 from 2 to 6 +move 1 from 7 to 1 +move 10 from 5 to 7 +move 15 from 1 to 4 +move 13 from 1 to 5 +move 3 from 6 to 8 +move 1 from 8 to 9 diff --git a/2022/src/day05.rs b/2022/src/day05.rs index 745aff3..83960cc 100644 --- a/2022/src/day05.rs +++ b/2022/src/day05.rs @@ -1,9 +1,169 @@ +use std::cmp::Ordering; + use anyhow::Result; +use nom::branch::alt; +use nom::bytes::complete::tag; +use nom::bytes::complete::take; +use nom::bytes::complete::take_until; +use nom::character::complete::newline; +use nom::combinator::map; +use nom::combinator::opt; +use nom::multi::fold_many1; +use nom::multi::many1; +use nom::sequence::delimited; +use nom::sequence::preceded; +use nom::sequence::terminated; +use nom::sequence::tuple; +use nom::IResult; -pub fn part1(_input: &[u8]) -> Result { - todo!() +use crate::common::parse_input; + +type Move = (usize, usize, usize); +type OwnedStacks = Vec>; + +fn parse_row<'a>(input: &'a [u8], stacks: &mut OwnedStacks) -> IResult<&'a [u8], ()> { + let mut index = 0usize; + + // Forgive me for this crime + fold_many1( + terminated( + alt(( + // Parse a delimited value into a Some(content) + map(delimited(tag("["), take(1usize), tag("]")), |v: &[u8]| { + Some(v[0]) + }), + // Or an empty stack into a None + map(tag(" "), |_| None), + )), + opt(tag(" ")), + ), + || (), + move |_, c| { + if let Some(b) = c { + if stacks.len() <= index { + stacks.resize_with(index + 1, Vec::new); + } + + stacks[index].push(b) + } + + index += 1; + }, + )(input) } -pub fn part2(_input: &[u8]) -> Result { - todo!() +fn parse_stacks(input: &[u8]) -> IResult<&[u8], OwnedStacks> { + let mut stacks = Vec::new(); + + let (input, _) = terminated( + fold_many1( + terminated(|input| parse_row(input, &mut stacks), newline), + || (), + |_, _| (), + ), + // Skip the line with the numbers + take_until("\n\n"), + )(input)?; + + // Reverse the stacks since we parsed them top-down + for stack in &mut stacks { + stack.reverse(); + } + + Ok((input, stacks)) +} + +fn parse_task(input: &[u8]) -> IResult<&[u8], (OwnedStacks, Vec)> { + fn parse_usize(input: &[u8]) -> IResult<&[u8], usize> { + map(nom::character::complete::u32, |v| v as usize)(input) + } + let (input, stacks) = parse_stacks(input)?; + + // Consume the double newline + let (input, _) = tag("\n\n")(input)?; + + let (input, moves) = many1(terminated( + tuple(( + preceded(tag("move "), parse_usize), + preceded(tag(" from "), parse_usize), + preceded(tag(" to "), parse_usize), + )), + newline, + ))(input)?; + + Ok((input, (stacks, moves))) +} + +/// Some magic to get two mutable references into the same slice +fn get_both(stacks: &mut [Vec], from: usize, to: usize) -> (&mut Vec, &mut Vec) { + match from.cmp(&to) { + Ordering::Greater => { + let (begin, end) = stacks.split_at_mut(from); + (&mut end[0], &mut begin[to]) + } + Ordering::Less => { + let (begin, end) = stacks.split_at_mut(to); + (&mut begin[from], &mut end[0]) + } + Ordering::Equal => panic!("Tried to stack from and to {from}"), + } +} + +fn compute_answer(stacks: &mut [Vec]) -> Result { + let mut result = String::with_capacity(stacks.len()); + + for stack in stacks { + result.push( + *stack + .last() + .ok_or_else(|| anyhow::anyhow!("Encountered empty stack"))? as char, + ); + } + + Ok(result) +} + +pub fn part1(input: &[u8]) -> Result { + let (mut stacks, moves) = parse_input(input, parse_task)?; + + for (count, from, to) in moves { + let (from, to) = get_both(&mut stacks, from - 1, to - 1); + + let drain_start = from.len() - count; + + to.extend(from.drain(drain_start..).rev()); + } + + compute_answer(&mut stacks) +} + +pub fn part2(input: &[u8]) -> Result { + let (mut stacks, moves) = parse_input(input, parse_task)?; + + for (count, from, to) in moves { + let (from, to) = get_both(&mut stacks, from - 1, to - 1); + + let drain_start = from.len() - count; + + to.extend(from.drain(drain_start..)); + } + + compute_answer(&mut stacks) +} + +#[cfg(test)] +mod tests { + use super::*; + + const SAMPLE: &[u8] = include_bytes!("samples/05.txt"); + + #[test] + fn sample_part1() { + assert_eq!(part1(SAMPLE).unwrap(), "CMZ"); + } + + #[test] + fn sample_part2() { + assert_eq!(part2(SAMPLE).unwrap(), "MCD"); + } } diff --git a/2022/src/samples/05.txt b/2022/src/samples/05.txt new file mode 100644 index 0000000..84933bb --- /dev/null +++ b/2022/src/samples/05.txt @@ -0,0 +1,9 @@ + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2