summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2023-12-16 09:34:25 +0200
committerJustin Wernick <justin@worthe-it.co.za>2023-12-16 09:34:25 +0200
commit00d27a850b47a7127f5e72a16e8d31333fc734a9 (patch)
tree878d61e7548b61a9c64dafd3612c08e642cff7c4
parentd989134cd57c4c3ba0e7a743dc6b0b193d9ec2ba (diff)
Day 16 part 2
-rw-r--r--2023/src/bin/day_16.rs56
1 files changed, 47 insertions, 9 deletions
diff --git a/2023/src/bin/day_16.rs b/2023/src/bin/day_16.rs
index f9614be..2c02d92 100644
--- a/2023/src/bin/day_16.rs
+++ b/2023/src/bin/day_16.rs
@@ -10,14 +10,19 @@ use std::{collections::BTreeSet, fs};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = fs::read_to_string("inputs/day_16.txt")?;
- let mut device = LightDevice::parser(&input).unwrap().1;
- device.energize();
- dbg!(&device.count_energized_blocks());
+ let device = LightDevice::parser(&input).unwrap().1;
+ {
+ let mut part_1_device = device.clone();
+ part_1_device.energize(Point { x: 0, y: 0 }, Direction::East);
+ dbg!(&part_1_device.count_energized_blocks());
+ }
+
+ dbg!(device.find_max_energization());
Ok(())
}
-#[derive(Debug)]
+#[derive(Debug, Clone)]
struct LightDevice {
mirrors: Vec<Vec<LightBlock>>,
light: Vec<Vec<BTreeSet<Direction>>>,
@@ -65,9 +70,9 @@ impl LightDevice {
)(input)
}
- fn energize(&mut self) {
- let mut frontier = vec![(Point { x: 0, y: 0 }, Direction::East)];
- self.light[0][0].insert(Direction::East);
+ fn energize(&mut self, start_point: Point, start_direction: Direction) {
+ let mut frontier = vec![(start_point.clone(), start_direction.clone())];
+ self.light[start_point.y][start_point.x].insert(start_direction);
while let Some((front_light_p, front_light_dir)) = frontier.pop() {
let mirror = self.mirrors[front_light_p.y][front_light_p.x];
@@ -104,8 +109,6 @@ impl LightDevice {
frontier.push((new_light_p, new_light_dir));
}
}
-
- // println!("{:?}", &frontier);
}
}
@@ -116,6 +119,41 @@ impl LightDevice {
.filter(|set| set.len() > 0)
.count()
}
+
+ fn find_max_energization(&self) -> usize {
+ (0..self.bounds.x)
+ .flat_map(|x| {
+ vec![
+ (Point { x, y: 0 }, Direction::South),
+ (
+ Point {
+ x,
+ y: self.bounds.y - 1,
+ },
+ Direction::North,
+ ),
+ ]
+ })
+ .chain((0..self.bounds.y).flat_map(|y| {
+ vec![
+ (Point { x: 0, y }, Direction::East),
+ (
+ Point {
+ x: self.bounds.x - 1,
+ y,
+ },
+ Direction::West,
+ ),
+ ]
+ }))
+ .map(|(start_p, start_d)| {
+ let mut energizer = self.clone();
+ energizer.energize(start_p, start_d);
+ energizer.count_energized_blocks()
+ })
+ .max()
+ .unwrap()
+ }
}
impl LightBlock {