From 72504d71ef505fa1dd62f1e6f2bfe9cfa3ba8280 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 23 Dec 2022 18:14:23 +0100 Subject: [PATCH] Implement 2022 day 23 part 2 --- 2022/inputs/23.txt | 73 +++++++++++++++++++++++++++++++++++++++++ 2022/src/day23.rs | 39 ++++++++++++++++------ 2022/src/samples/23.txt | 12 +++++++ 3 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 2022/inputs/23.txt create mode 100644 2022/src/samples/23.txt diff --git a/2022/inputs/23.txt b/2022/inputs/23.txt new file mode 100644 index 0000000..b5828cc --- /dev/null +++ b/2022/inputs/23.txt @@ -0,0 +1,73 @@ +#.##.##...####..#.##.##....###....##...#..#####..#.##.##.#...###.##..#.## +.#....#..#.##..#.#.#...#..#..####..#.#.##.#.#..#.#..#.#..##.#..........#. +##.#####.###.#...######..###.#.#..##.#.##.#......#..##.....###.#.##.##.## +..#######.####.#..###...####..####....#.####.#.#..#.#.###.##.###.##..#... +.#..#.##...#...####.....##.#.#..#...###.....##..#.##..#.#.#.##.##.#.#.#.. +####.#...#.#..##..###.####.#######.....#.##.#..#.#...#.##.#....#......... +..#.#.####...###.##.#..#.....#.##...#.#.####.....#..##....#..#####...##.. +#####..##.....#.##..#.####....#.#....#.##....#.#....##.#####.#...####..## +#.#..#####.###..####..#.##..#######....##..#.###.#.#....##.#..##.##..#..# +..##.#.##.....###..#..#..#.####....#..####..##.###...#..#..#...##..##..## +#.###.##.#...##.##.#.##..####.##.#.#.###...##.#....#.#.#.##.##...#.##.#.# +.#####..###.#.###.###....#..#..#....#.##.#.###.#.####..###.#####.#.#####. +...#..##.##.#.#.###.#######.##.#...##..##...#......#..####.#.#...#.#####. +.#..###..######..#.#.##.##.#.##....##.#...#...#..#.#.##.#...#.#..#...#.## +##..#.#...#.###.#..#..#.###.###..###.#.......#####.##.####..#...#..#....# +...#..###...#...###.#.....#.###.##.###.####.##.....##..##.#.##.#.#...#.## +####.#......#..###..#.#...##.#.##..#...#.###...#..##.#..#.....####.##.### +##....#..#####..#####....##.#####.##.###..#.#####..###.##..#.#..###.#.### +##.##...##.###.#.#..#####...#..#...#....#..#.#.#.......##..#...#..######. +......#.###.#.##..####..#..##..##.###.#..##...#...#...#...#.###.#.#.##.## +....##..#...##.##.#.#.#..#..#.#...####.#.....#.#.#.##.#.....##.#...#.#.## +..#..###.#..###.####..#.#....#..##.#.##..###...#.##.#.####.##.#.#.#.#...# +...###.....####.######...####.....#.#.#.#...#..##.##...#.###..###.#.##### +#.##..#.##.#......#####..#...####.#...###...###.##...##.##...###.###.##.# +###....#..#.###..#.####.##.#...##..#..#..#....###...#......#..#..##.#.### +.####.#.###..#########...#.##############.##...#.##.##.#..#.#.##.#.#.###. +.#.##...#...###..#..#.#.#..#.#.#.#.##........#.##.#.#.##.##.#.#.##.###### +##.#.#.###..###.###.#.#.....#.###..####.#.#..##.#.##..#.####..#.......##. +.#.#####.#..###..##.#..#...#.#.##..#..#..#..##.####.##.#..###.#......#... +.##..##..#..#.#..###...#.#.##.###..#..#...#.##.#.##..#.#.##..#....#...#.. +..#......###..###..##...##.###..##.#.....#.###...##.##..#...#..#.#..#.... +.#.#.#.#.#...###.#######..#..###...#.###.#.###.##....#...#.##......####.. +#...#.##.#...#.#.#.###.#..#.#..##.###.######.#.#....###.##...#....#....#. +.###....#..####.#..#....##.#..##..#..#.#####.#.#...##.##...####.###.##..# +#...#.#...#...##.#####.#.#...##..#..##.#...#...###....##.######.#..#.##.# +#...##...##..#......####.#.#.##.##.#.......####...####...#..#..#.#..#.##. +.###....#.####..##.###..#....#...##.###..#...#.##.#...#####.###..#..#..## +##.#..##..###.#..#..####...#.##.##..#......####....#....#.###..##......## +##.##....#####.##.###.######..#..####..##.#.####.#..####.##...##....##### +.##.#..##..#.##..###.....#..#.##..####...#.##...#.#.#.#..##.###.....##... +.##.#...#...####..##.#....##...#..###.#.#..#....##.######.#...##.###..##. +##.##.###..#.##.#..#....#.##.#.##..#...#..##.#.####..##.##...#..##.##..## +.#..###.#.#.##..#..#.####.#.##.#..##.#....#.###.##..#.###...#..#....###.. +####..#.#..###..#...##.##.###.##...#.##.##...##..#.##.##...#.#..#.#.#.#.. +..###...#..#...........#.###.##..#..###..##.#.##.#...#.#..#.....##..##### +.....##....###..###.###..#..#...##..#......#.##..#.#.#..##..#.###.....#.# +..####.#......#...#.#.#.#..#..##....##..#.#...##.##.##.###.###...###...#. +##..#.#.#.##.###########.#.###....####.##.#.#...####..#.##.#...##...#.... +##...#..#...####.#.####....#.########.######..###....###..##..#...#.###.. +##.###.####..#.##.#.#####.##.###..#.#.#.##.##.##.####.##.#...#..#..##.... +.#..#.#.##..##...#....#.#....#....###..#..##..###.##.####.##.####.##.#... +##.#.##.##...#.#..#.#...##...#...#..#......#.###......###.....#.#.#####.# +..#.##.##.###....#.#..###..#..##.####.###.##..##.##....#...##.#..#..##.## +#####.#...#..##..##.#.#.....##..#..##.##.##.####..#####.#..###..#.##..##. +####.##..........###.###.#####.#.#..##...##.....#..###..##..#.####..#.##. +#.....##.####...#..##.##..#..#....##..#...#..#.#.....#..#...#.###.##.#..# +#.#.##..###.#...##.#...###.#......#....##..##.##.###.#.#.#..#.##.####..## +##..###.#.#......##.##.##.#.#####.#.#.###...###.##.#......#.##########..# +.####....#..#...###.#..##.#.####..#.#..######.#.####..##....###..##..#..# +#..##.#.#...##.#.#.########.....#.##...#....#.####.###.########.#.##.#... +#.#######..#.##.#.#.#..####..##.#..###.####.#..#########....####....###.. +..##.#...##...#..###.#..##.#..#...##.####.######.####.#..##.#...#.#..###. +.#..##.#.##..#......##.#.#.##.#####.#.###....#...###...#....#.###.....### +#......#.#..##....##.########.#..#..##.##......#.#..######..##.##..##.### +..##...#...#.#..#.#########..##..#####..#..#..##.#....##...#.#....##.#.#. +.##..###.####.#..#####.###..###..#......##...#.....####..###..#..##.#.##. +......###.#..#...#..#.#.##.#.###...##.#.#....####.#..#.######.#.#...####. +...#.##.####....##..##.#.###.###.....#..###...####..##..######.#..##..... +.....####.....###.#.#.......#####.#.#....##...#####..#..#.#..#.#.#..##.## +##..##.#..##..#..#.#####..##.####.#...#.#..#..###..#..#....#...#.###.##.. +#.#..#.#########.####..#####..####.#.#####....#.#..##...#..####.#..###.#. +#..##.###.##..#....#...#..#.##..#..###..#####..#######.#####....#.##..... +####.####.########.#.###....#.#..#..#.#.#.#..#....#..#.....#..##..#.##.#. diff --git a/2022/src/day23.rs b/2022/src/day23.rs index 083bfaf..70aa98b 100644 --- a/2022/src/day23.rs +++ b/2022/src/day23.rs @@ -90,11 +90,23 @@ fn print(elves: &AHashSet) -> Result<()> { pub fn part1(input: &[u8]) -> Result { let mut elves = parse_input(input, parse_elves)?; + simulate(&mut elves, 10); + + let (x_bounds, y_bounds) = determine_bounding_box(&elves)?; + + let area = (x_bounds.end() - x_bounds.start() + 1) * (y_bounds.end() - y_bounds.start() + 1); + + let free = area - elves.len() as i32; + + Ok(free.to_string()) +} + +fn simulate(elves: &mut AHashSet, max: usize) -> Option { let mut todo = Vec::new(); let mut to_return = Vec::new(); let mut origin = AHashMap::new(); - for it in 0..10 { + for it in 0..max { // Remove all todos from a previous iteration todo.clear(); @@ -148,6 +160,10 @@ pub fn part1(input: &[u8]) -> Result { } } + if origin.is_empty() { + return Some(it + 1); + } + // Remove entries we processed for elf in &todo { elves.remove(elf); @@ -160,17 +176,15 @@ pub fn part1(input: &[u8]) -> Result { elves.extend(origin.drain().map(|(dest, _)| dest)); } - let (x_bounds, y_bounds) = determine_bounding_box(&elves)?; - - let area = (x_bounds.end() - x_bounds.start() + 1) * (y_bounds.end() - y_bounds.start() + 1); - - let free = area - elves.len() as i32; - - Ok(free.to_string()) + None } -pub fn part2(_input: &[u8]) -> Result { - anyhow::bail!("not implemented") +pub fn part2(input: &[u8]) -> Result { + let mut elves = parse_input(input, parse_elves)?; + + let first_non_moved = simulate(&mut elves, usize::MAX).context("Elves didn't stop moving?")?; + + Ok(first_non_moved.to_string()) } #[cfg(test)] @@ -183,4 +197,9 @@ mod tests { fn sample_part1() { assert_eq!(part1(SAMPLE).unwrap(), "110"); } + + #[test] + fn sample_part2() { + assert_eq!(part2(SAMPLE).unwrap(), "20"); + } } diff --git a/2022/src/samples/23.txt b/2022/src/samples/23.txt new file mode 100644 index 0000000..c84aa7c --- /dev/null +++ b/2022/src/samples/23.txt @@ -0,0 +1,12 @@ +.............. +.............. +.......#...... +.....###.#.... +...#...#.#.... +....#...##.... +...#.###...... +...##.#.##.... +....#..#...... +.............. +.............. +..............