summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2022-12-23 15:36:01 +0200
committerJustin Wernick <justin@worthe-it.co.za>2022-12-23 15:36:01 +0200
commit0071f556d6b6fdac0fb6c7879e92932d80418f0f (patch)
treee68531a30cf818afe008fb4aa54be252f054307f
parent99e87a92e8c0d5faa8e928ad35f4931b220c908f (diff)
Day 23 complete
-rw-r--r--2022/src/bin/day_23.rs209
1 files changed, 188 insertions, 21 deletions
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<dyn std::error::Error>> {
- 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<Point>,
check_order: VecDeque<Direction>,
@@ -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<Point, Point> = BTreeMap::new();
let mut conflict_moves: BTreeSet<Point> = 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<Point> {
@@ -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
+ );
+}