@@ -24,21 +24,21 @@ impl Solution for Day07 {
2424 let mut splits = 0 ;
2525
2626 while let Some ( current_beam) = current_beams. pop_front ( ) {
27+ if finished_beams
28+ . iter ( )
29+ . chain ( current_beams. iter ( ) )
30+ . any ( |beam| beam. collides ( & current_beam) )
31+ {
32+ continue ;
33+ }
34+
2735 let down = current_beam. down ( ) ;
2836
2937 if splitters. contains ( & down. current ( ) ) {
3038 finished_beams. push ( current_beam) ;
3139 splits += 1 ;
3240
3341 for split in down. split ( ) {
34- if finished_beams
35- . iter ( )
36- . chain ( current_beams. iter ( ) )
37- . any ( |beam| beam. collides ( & split) )
38- {
39- continue ;
40- }
41-
4242 current_beams. push_back ( split) ;
4343 }
4444
@@ -53,8 +53,6 @@ impl Solution for Day07 {
5353 current_beams. push_front ( down) ;
5454 }
5555
56- print ( & grid, & finished_beams) ;
57-
5856 splits. to_string ( )
5957 }
6058
@@ -66,9 +64,8 @@ impl Solution for Day07 {
6664impl Day07 {
6765 fn parse ( & self , input : & str ) -> Grid < char > {
6866 let without_redundant_lines = input. lines ( ) . step_by ( 2 ) . collect :: < Vec < _ > > ( ) . join ( "\n " ) ;
69- let input = without_redundant_lines. as_str ( ) ;
7067
71- Grid :: from ( input )
68+ Grid :: from ( without_redundant_lines . as_str ( ) )
7269 }
7370}
7471
@@ -89,15 +86,12 @@ struct Beam {
8986
9087impl Beam {
9188 fn collides ( & self , other : & Self ) -> bool {
92- let other_start = other. line . start ( ) ;
93- if other_start != other. line . end ( ) {
94- panic ! ( "We only support beam that just started" ) ;
95- }
89+ let other = other. current ( ) ;
9690
9791 let start = self . line . start ( ) ;
9892 let end = self . line . end ( ) ;
9993
100- start. x == other_start . x && ( start. y ..=end. y ) . contains ( & other_start . y )
94+ start. x == other . x && ( start. y ..=end. y ) . contains ( & other . y )
10195 }
10296
10397 fn down ( & self ) -> Self {
@@ -219,6 +213,22 @@ mod tests {
219213 assert_eq ! ( "3" , Day07 . part_one( EXAMPLE_FROM_REDDIT ) ) ;
220214 }
221215
216+ const EXAMPLE_FROM_REDDIT2 : & str = r#"..S..
217+ .....
218+ ..^..
219+ .....
220+ ...^.
221+ .....
222+ .^...
223+ .....
224+ ..^..
225+ ....."# ;
226+
227+ #[ test]
228+ fn part_one_example_from_reddit2 ( ) {
229+ assert_eq ! ( "4" , Day07 . part_one( EXAMPLE_FROM_REDDIT2 ) ) ;
230+ }
231+
222232 const MY_EXAMPLE : & str = r#"..S..
223233.....
224234..^..
0 commit comments