@@ -10,19 +10,29 @@ impl Solution for Day06 {
1010 fn part_one ( & self , input : & str ) -> String {
1111 let mut grid = Grid :: filled (
1212 SurfaceRange :: from ( ( Point :: new ( 0 , 0 ) , Point :: new ( 999 , 999 ) ) ) ,
13- false ,
13+ 0 ,
1414 ) ;
1515 let instructions = self . parse ( input) ;
1616
1717 for instruction in instructions {
18- instruction. apply ( & mut grid) ;
18+ instruction. apply_part_one ( & mut grid) ;
1919 }
2020
21- grid. get_all_positions ( & true ) . len ( ) . to_string ( )
21+ grid. get_all_positions ( & 1 ) . len ( ) . to_string ( )
2222 }
2323
24- fn part_two ( & self , _input : & str ) -> String {
25- String :: from ( "0" )
24+ fn part_two ( & self , input : & str ) -> String {
25+ let mut grid = Grid :: filled (
26+ SurfaceRange :: from ( ( Point :: new ( 0 , 0 ) , Point :: new ( 999 , 999 ) ) ) ,
27+ 0 ,
28+ ) ;
29+ let instructions = self . parse ( input) ;
30+
31+ for instruction in instructions {
32+ instruction. apply_part_two ( & mut grid) ;
33+ }
34+
35+ grid. all ( ) . values ( ) . sum :: < u64 > ( ) . to_string ( )
2636 }
2737}
2838
@@ -52,7 +62,8 @@ impl Day06 {
5262}
5363
5464trait Instruction : Debug {
55- fn apply ( & self , grid : & mut Grid < bool > ) ;
65+ fn apply_part_one ( & self , grid : & mut Grid < u64 > ) ;
66+ fn apply_part_two ( & self , grid : & mut Grid < u64 > ) ;
5667}
5768
5869#[ derive( Debug ) ]
@@ -69,8 +80,12 @@ impl From<(Point, Point)> for TurnOn {
6980}
7081
7182impl Instruction for TurnOn {
72- fn apply ( & self , grid : & mut Grid < bool > ) {
73- grid. modify_many ( self . surface_range . points ( ) , true )
83+ fn apply_part_one ( & self , grid : & mut Grid < u64 > ) {
84+ grid. modify_many ( self . surface_range . points ( ) , 1 )
85+ }
86+
87+ fn apply_part_two ( & self , grid : & mut Grid < u64 > ) {
88+ grid. modify_many_with ( self . surface_range . points ( ) , |b| * b += 1 )
7489 }
7590}
7691
@@ -88,8 +103,12 @@ impl From<(Point, Point)> for TurnOff {
88103}
89104
90105impl Instruction for TurnOff {
91- fn apply ( & self , grid : & mut Grid < bool > ) {
92- grid. modify_many ( self . surface_range . points ( ) , false )
106+ fn apply_part_one ( & self , grid : & mut Grid < u64 > ) {
107+ grid. modify_many ( self . surface_range . points ( ) , 0 )
108+ }
109+
110+ fn apply_part_two ( & self , grid : & mut Grid < u64 > ) {
111+ grid. modify_many_with ( self . surface_range . points ( ) , |b| * b = u64:: max ( * b - 1 , 0 ) )
93112 }
94113}
95114
@@ -107,8 +126,14 @@ impl From<(Point, Point)> for Toggle {
107126}
108127
109128impl Instruction for Toggle {
110- fn apply ( & self , grid : & mut Grid < bool > ) {
111- grid. modify_many_with ( self . surface_range . points ( ) , |b| * b = !* b)
129+ fn apply_part_one ( & self , grid : & mut Grid < u64 > ) {
130+ grid. modify_many_with ( self . surface_range . points ( ) , |b| {
131+ * b = if * b == 0 { 1 } else { 0 }
132+ } )
133+ }
134+
135+ fn apply_part_two ( & self , grid : & mut Grid < u64 > ) {
136+ grid. modify_many_with ( self . surface_range . points ( ) , |b| * b += 2 )
112137 }
113138}
114139
@@ -121,4 +146,10 @@ mod tests {
121146 assert_eq ! ( "1000000" , Day06 . part_one( "turn on 0,0 through 999,999" ) ) ;
122147 assert_eq ! ( "1000" , Day06 . part_one( "toggle 0,0 through 999,0" ) ) ;
123148 }
149+
150+ #[ test]
151+ fn part_two_example_test ( ) {
152+ assert_eq ! ( "1" , Day06 . part_two( "turn on 0,0 through 0,0" ) ) ;
153+ assert_eq ! ( "2000000" , Day06 . part_two( "toggle 0,0 through 999,999" ) ) ;
154+ }
124155}
0 commit comments