From 0071f556d6b6fdac0fb6c7879e92932d80418f0f Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Fri, 23 Dec 2022 15:36:01 +0200 Subject: Day 23 complete --- 2022/src/bin/day_23.rs | 209 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 188 insertions(+), 21 deletions(-) (limited to '2022/src/bin/day_23.rs') diff --git a/2022/src/bin/day_23.rs b/2022/src/bin/day_23.rs index 5f58dea..1267431 100644 --- a/2022/src/bin/day_23.rs +++ b/2022/src/bin/day_23.rs @@ -12,21 +12,31 @@ use std::{ }; fn main() -> Result<(), Box> { - let input = fs::read_to_string("inputs/day_23_test.txt")?; - let mut elves = ElfMap::parser(&input).unwrap().1; + let input = fs::read_to_string("inputs/day_23.txt")?; + let elves = ElfMap::parser(&input).unwrap().1; - dbg!(&elves); - for _ in 0..10 { - elves.process_round(); - dbg!(&elves); + { + let mut elves_1 = elves.clone(); + for _ in 0..10 { + elves_1.process_round(); + } + dbg!(elves_1.count_empty_ground()); } - dbg!(elves.count_empty_ground()); + { + let mut elves_2 = elves.clone(); + for round in 1.. { + if !elves_2.process_round() { + dbg!(round); + break; + } + } + } Ok(()) } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] struct ElfMap { elves: BTreeSet, check_order: VecDeque, @@ -81,7 +91,7 @@ impl ElfMap { )(input) } - fn process_round(&mut self) { + fn process_round(&mut self) -> bool { // key is destination! let mut elf_moves: BTreeMap = BTreeMap::new(); let mut conflict_moves: BTreeSet = BTreeSet::new(); @@ -98,6 +108,8 @@ impl ElfMap { } } + let any_elf_moved = elf_moves.len() > 0; + // phase 2: move the elves for (dest, src) in elf_moves { self.elves.remove(&src); @@ -109,6 +121,8 @@ impl ElfMap { .pop_front() .expect("Where did the directions go?"); self.check_order.push_back(rotate); + + any_elf_moved } fn find_elf_move(&self, elf: &Point) -> Option { @@ -133,16 +147,13 @@ impl ElfMap { } fn count_empty_ground(&self) -> i64 { - let mut min_x = 0; - let mut min_y = 0; - let mut max_x = 0; - let mut max_y = 0; - for elf in &self.elves { - min_x = min_x.min(elf.x); - min_y = min_y.min(elf.y); - max_x = max_x.max(elf.x); - max_y = max_y.max(elf.y); - } + let elf_x = self.elves.iter().map(|elf| elf.x); + let min_x = elf_x.clone().min().unwrap_or(0); + let max_x = elf_x.max().unwrap_or(0); + + let elf_y = self.elves.iter().map(|elf| elf.y); + let min_y = elf_y.clone().min().unwrap_or(0); + let max_y = elf_y.max().unwrap_or(0); let all_ground = (max_x - min_x + 1) * (max_y - min_y + 1); let covered_ground = self.elves.len() as i64; @@ -156,8 +167,8 @@ impl Point { None => ((-1..=1), (-1..=1)), Some(Direction::North) => ((-1..=-1), (-1..=1)), Some(Direction::South) => ((1..=1), (-1..=1)), - Some(Direction::East) => ((-1..=1), (-1..=-1)), - Some(Direction::West) => ((-1..=1), (1..=1)), + Some(Direction::West) => ((-1..=1), (-1..=-1)), + Some(Direction::East) => ((-1..=1), (1..=1)), }; y_range @@ -171,3 +182,159 @@ impl Point { .collect() } } + +#[test] +fn follows_the_example() { + let mut elf_map = ElfMap::parser( + r".............. +.............. +.......#...... +.....###.#.... +...#...#.#.... +....#...##.... +...#.###...... +...##.#.##.... +....#..#...... +.............. +.............. +..............", + ) + .unwrap() + .1; + + elf_map.process_round(); + assert_eq!( + elf_map.elves, + ElfMap::parser( + r".............. +.......#...... +.....#...#.... +...#..#.#..... +.......#..#... +....#.#.##.... +..#..#.#...... +..#.#.#.##.... +.............. +....#..#...... +.............. +.............. +" + ) + .unwrap() + .1 + .elves + ); + + elf_map.process_round(); + assert_eq!( + elf_map.elves, + ElfMap::parser( + r".............. +.......#...... +....#.....#... +...#..#.#..... +.......#...#.. +...#..#.#..... +.#...#.#.#.... +.............. +..#.#.#.##.... +....#..#...... +.............. +.............. +" + ) + .unwrap() + .1 + .elves + ); + + elf_map.process_round(); + assert_eq!( + elf_map.elves, + ElfMap::parser( + r".............. +.......#...... +.....#....#... +..#..#...#.... +.......#...#.. +...#..#.#..... +.#..#.....#... +.......##..... +..##.#....#... +...#.......... +.......#...... +.............." + ) + .unwrap() + .1 + .elves + ); + + elf_map.process_round(); + assert_eq!( + elf_map.elves, + ElfMap::parser( + r".............. +.......#...... +......#....#.. +..#...##...... +...#.....#.#.. +.........#.... +.#...###..#... +..#......#.... +....##....#... +....#......... +.......#...... +.............." + ) + .unwrap() + .1 + .elves + ); + + elf_map.process_round(); + assert_eq!( + elf_map.elves, + ElfMap::parser( + r".......#...... +.............. +..#..#.....#.. +.........#.... +......##...#.. +.#.#.####..... +...........#.. +....##..#..... +..#........... +..........#... +....#..#...... +.............." + ) + .unwrap() + .1 + .elves + ); + + for _ in 0..5 { + elf_map.process_round(); + } + assert_eq!( + elf_map.elves, + ElfMap::parser( + r".......#...... +...........#.. +..#.#..#...... +......#....... +...#.....#..#. +.#......##.... +.....##....... +..#........#.. +....#.#..#.... +.............. +....#..#..#... +.............." + ) + .unwrap() + .1 + .elves + ); +} -- cgit v1.2.3