From 00d27a850b47a7127f5e72a16e8d31333fc734a9 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Sat, 16 Dec 2023 09:34:25 +0200 Subject: Day 16 part 2 --- 2023/src/bin/day_16.rs | 56 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file 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> { 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>, light: Vec>>, @@ -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 { -- cgit v1.2.3