@@ -3,6 +3,7 @@ use crate::utils::grid::{Grid, PrintableOnGrid};
33use crate :: utils:: line:: Line ;
44use crate :: utils:: point:: Point ;
55use std:: collections:: { HashSet , VecDeque } ;
6+ use std:: fmt:: { Debug , Display , Formatter } ;
67
78const START : char = 'S' ;
89const SPLITTER : char = '^' ;
@@ -27,30 +28,28 @@ impl Solution for Day07 {
2728 if splitters. contains ( & down. current ( ) ) {
2829 result_beams. push ( current_beam) ;
2930
30- for other in down. split ( ) {
31- if result_beams. iter ( ) . any ( |beam| beam. is_on ( & other ) ) {
31+ for split in down. split ( ) {
32+ if result_beams. iter ( ) . any ( |beam| beam. collides ( & split ) ) {
3233 continue ;
3334 }
3435
35- current_beams. push_back ( other) ;
36+ if current_beams. iter ( ) . any ( |beam| beam. collides ( & split) ) {
37+ continue ;
38+ }
39+
40+ current_beams. push_back ( split) ;
3641 }
3742
3843 continue ;
3944 }
4045
4146 if down. current ( ) . y > max_height + 1 {
42- result_beams. push ( current_beam) ;
4347 continue ;
4448 }
4549
4650 current_beams. push_front ( down) ;
4751 }
4852
49- let mut new = grid. clone ( ) ;
50- new. print ( & result_beams[ ..] ) ;
51-
52- println ! ( "{}" , new) ;
53-
5453 result_beams. len ( ) . to_string ( )
5554 }
5655
@@ -59,18 +58,36 @@ impl Solution for Day07 {
5958 }
6059}
6160
62- #[ derive( Copy , Clone ) ]
61+ #[ allow( dead_code) ]
62+ fn print ( grid : & Grid < char > , beams : & [ Beam ] ) {
63+ let mut new = grid. clone ( ) ;
64+ new. print ( beams) ;
65+
66+ println ! ( "{}" , new) ;
67+ println ! ( "{} beams" , beams. len( ) ) ;
68+ println ! ( ) ;
69+ }
70+
71+ #[ derive( Copy , Clone , Debug ) ]
6372struct Beam {
6473 line : Line ,
6574}
6675
6776impl Beam {
68- fn is_on ( & self , other : & Self ) -> bool {
69- if other. line . start ( ) != other. line . end ( ) {
77+ fn collides ( & self , other : & Self ) -> bool {
78+ let other_start = other. line . start ( ) ;
79+ if other_start != other. line . end ( ) {
7080 panic ! ( "We only support beam that just started" ) ;
7181 }
7282
73- self . line . is_on ( & other. line . start ( ) )
83+ let start = self . line . start ( ) ;
84+ let end = self . line . end ( ) ;
85+
86+ if start. x != other_start. x {
87+ return false ;
88+ }
89+
90+ start. y <= other_start. y && end. y >= other_start. y
7491 }
7592
7693 fn down ( & self ) -> Self {
@@ -96,26 +113,41 @@ impl From<Point> for Beam {
96113 }
97114}
98115
116+ impl From < ( Point , Point ) > for Beam {
117+ fn from ( value : ( Point , Point ) ) -> Self {
118+ Self {
119+ line : Line :: new ( value. 0 , value. 1 ) ,
120+ }
121+ }
122+ }
123+
124+ impl Display for Beam {
125+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
126+ write ! ( f, "({}) - ({})" , self . line. start( ) , self . line. end( ) )
127+ }
128+ }
129+
99130impl PrintableOnGrid for Beam {
100131 type Cell = char ;
101132
102133 fn print_on_grid ( & self , grid : & mut Grid < char > ) {
103- let mut moved = self . line . start ( ) ;
134+ let mut current = self . line . start ( ) ;
104135
105136 loop {
106- grid. modify ( moved , BEAM ) ;
107- if moved == self . line . end ( ) {
137+ grid. modify ( current , BEAM ) ;
138+ if current == self . line . end ( ) {
108139 break ;
109140 }
110- moved = moved . south ( ) ;
141+ current = current . south ( ) ;
111142 }
112143 }
113144}
114145
115146#[ cfg( test) ]
116147mod tests {
117- use crate :: solutions:: year2025:: day07:: Day07 ;
148+ use crate :: solutions:: year2025:: day07:: { Beam , Day07 } ;
118149 use crate :: solutions:: Solution ;
150+ use crate :: utils:: point:: Point ;
119151
120152 const EXAMPLE : & str = r#".......S.......
121153...............
@@ -138,4 +170,28 @@ mod tests {
138170 fn part_one_example_test ( ) {
139171 assert_eq ! ( "21" , Day07 . part_one( EXAMPLE ) ) ;
140172 }
173+
174+ #[ test]
175+ fn beam_collides ( ) {
176+ let beam: Beam = ( Point :: new ( 3 , 0 ) , Point :: new ( 3 , 3 ) ) . into ( ) ;
177+
178+ assert ! ( !beam. collides( & Beam :: from( Point :: new( 3 , -1 ) ) ) ) ;
179+ assert ! ( beam. collides( & Beam :: from( Point :: new( 3 , 0 ) ) ) ) ;
180+ assert ! ( beam. collides( & Beam :: from( Point :: new( 3 , 1 ) ) ) ) ;
181+ assert ! ( beam. collides( & Beam :: from( Point :: new( 3 , 2 ) ) ) ) ;
182+ assert ! ( beam. collides( & Beam :: from( Point :: new( 3 , 3 ) ) ) ) ;
183+ assert ! ( !beam. collides( & Beam :: from( Point :: new( 3 , 4 ) ) ) ) ;
184+
185+ assert ! ( !beam. collides( & Beam :: from( Point :: new( 2 , 0 ) ) ) ) ;
186+ assert ! ( !beam. collides( & Beam :: from( Point :: new( 2 , 1 ) ) ) ) ;
187+ assert ! ( !beam. collides( & Beam :: from( Point :: new( 2 , 2 ) ) ) ) ;
188+ assert ! ( !beam. collides( & Beam :: from( Point :: new( 2 , 3 ) ) ) ) ;
189+ assert ! ( !beam. collides( & Beam :: from( Point :: new( 2 , 4 ) ) ) ) ;
190+
191+ assert ! ( !beam. collides( & Beam :: from( Point :: new( 4 , 0 ) ) ) ) ;
192+ assert ! ( !beam. collides( & Beam :: from( Point :: new( 4 , 1 ) ) ) ) ;
193+ assert ! ( !beam. collides( & Beam :: from( Point :: new( 4 , 2 ) ) ) ) ;
194+ assert ! ( !beam. collides( & Beam :: from( Point :: new( 4 , 3 ) ) ) ) ;
195+ assert ! ( !beam. collides( & Beam :: from( Point :: new( 4 , 4 ) ) ) ) ;
196+ }
141197}
0 commit comments