Skip to content

Commit e254be3

Browse files
committed
Day 16, part 2: Easy enough to just do it.
1 parent 8d06f9b commit e254be3

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

src/day16.rs

+43-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,45 @@ impl std::str::FromStr for Contraption {
4343
}
4444

4545
impl Contraption {
46-
pub fn simulate_light_beam(&self, print: bool, stop_after: Option<usize>) -> usize {
46+
pub fn simulate_all_light_beams(&self) -> usize {
47+
let mut result = 0;
48+
let mut best = None;
49+
// Simulate beams coming from left and right
50+
for y in 0..self.tiles.len() {
51+
let mut source = (0, y, Direction::Right);
52+
let mut num = self.simulate_light_beam(source, false, None);
53+
if num > result {
54+
result = num;
55+
best = Some(source);
56+
}
57+
source = (self.tiles[y].len() - 1, y, Direction::Left);
58+
num = self.simulate_light_beam(source, false, None);
59+
if num > result {
60+
result = num;
61+
best = Some(source);
62+
}
63+
}
64+
// Simulate beams coming from top and bottom
65+
for x in 0..self.tiles[0].len() {
66+
let mut source = (x, 0, Direction::Down);
67+
let mut num = self.simulate_light_beam(source, false, None);
68+
if num > result {
69+
result = num;
70+
best = Some(source);
71+
}
72+
source = (x, self.tiles.len() - 1, Direction::Up);
73+
num = self.simulate_light_beam(source, false, None);
74+
if num > result {
75+
result = num;
76+
best = Some(source);
77+
}
78+
}
79+
80+
println!("best = {:?}", best);
81+
result
82+
}
83+
84+
pub fn simulate_light_beam(&self, source: (usize, usize, Direction), print: bool, stop_after: Option<usize>) -> usize {
4785
let mut beam_heads: Vec<(usize, usize, Direction)> = vec![];
4886

4987
// Create a field of the same size as the contraption and track the direction of each beam
@@ -57,7 +95,7 @@ impl Contraption {
5795

5896
// Start with one beam in the top left corner, pointing right:
5997
let mut steps = 0;
60-
beam_heads.push((0, 0, Direction::Right));
98+
beam_heads.push(source);
6199
while let Some((x, y, direction)) = beam_heads.pop() {
62100
if print {
63101
println!("beam at ({}, {}) {:?}", x, y, direction);
@@ -199,7 +237,8 @@ pub fn main() {
199237
match std::fs::read_to_string("day16.input") {
200238
Ok(input) => {
201239
if let Ok(contraption) = input.parse::<Contraption>() {
202-
println!("part1 = {}", contraption.simulate_light_beam(true, None));
240+
println!("part1 = {}", contraption.simulate_light_beam((0, 0, Direction::Right), false, None));
241+
println!("part2 = {}", contraption.simulate_all_light_beams());
203242
} else {
204243
println!("error parsing input");
205244
}
@@ -225,6 +264,6 @@ mod tests {
225264

226265
#[test]
227266
fn part1() {
228-
assert_eq!(EXAMPLE.parse::<Contraption>().unwrap().simulate_light_beam(true, Some(200)), 46);
267+
assert_eq!(EXAMPLE.parse::<Contraption>().unwrap().simulate_light_beam((0, 0, Direction::Right), true, Some(200)), 46);
229268
}
230269
}

0 commit comments

Comments
 (0)