@@ -43,7 +43,45 @@ impl std::str::FromStr for Contraption {
43
43
}
44
44
45
45
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 {
47
85
let mut beam_heads: Vec < ( usize , usize , Direction ) > = vec ! [ ] ;
48
86
49
87
// Create a field of the same size as the contraption and track the direction of each beam
@@ -57,7 +95,7 @@ impl Contraption {
57
95
58
96
// Start with one beam in the top left corner, pointing right:
59
97
let mut steps = 0 ;
60
- beam_heads. push ( ( 0 , 0 , Direction :: Right ) ) ;
98
+ beam_heads. push ( source ) ;
61
99
while let Some ( ( x, y, direction) ) = beam_heads. pop ( ) {
62
100
if print {
63
101
println ! ( "beam at ({}, {}) {:?}" , x, y, direction) ;
@@ -199,7 +237,8 @@ pub fn main() {
199
237
match std:: fs:: read_to_string ( "day16.input" ) {
200
238
Ok ( input) => {
201
239
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( ) ) ;
203
242
} else {
204
243
println ! ( "error parsing input" ) ;
205
244
}
@@ -225,6 +264,6 @@ mod tests {
225
264
226
265
#[ test]
227
266
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 ) ;
229
268
}
230
269
}
0 commit comments