1
- use std:: fmt:: { Debug , Write } ;
2
-
3
1
use aoc_lib:: { direction:: cardinal:: Direction , matrix:: Matrix } ;
4
2
use common:: { solution, Answer } ;
5
3
use nd_vec:: { vector, Vec2 } ;
@@ -46,18 +44,22 @@ impl Problem {
46
44
_ => panic ! ( ) ,
47
45
} ) ;
48
46
47
+ // For part B, double the width of the board and convert the previously
48
+ // one tile boxes into a Tile::Box and Tile::BoxRight
49
49
if part_b {
50
50
board. size = vector ! ( board. size. x( ) * 2 , board. size. y( ) ) ;
51
- let mut new = Vec :: new ( ) ;
52
- for data in board. data {
53
- new. push ( data) ;
54
- new. push ( match data {
55
- Tile :: Box => Tile :: BoxRight ,
56
- Tile :: Robot => Tile :: Empty ,
57
- _ => data,
58
- } ) ;
51
+ let mut i = 0 ;
52
+ while i < board. data . len ( ) {
53
+ board. data . insert (
54
+ i + 1 ,
55
+ match board. data [ i] {
56
+ Tile :: Box => Tile :: BoxRight ,
57
+ Tile :: Robot => Tile :: Empty ,
58
+ x => x,
59
+ } ,
60
+ ) ;
61
+ i += 2 ;
59
62
}
60
- board. data = new;
61
63
}
62
64
63
65
let instructions = instructions
@@ -86,9 +88,7 @@ impl Problem {
86
88
}
87
89
88
90
fn tick_all ( & mut self , part_b : bool ) {
89
- for _ in 0 ..self . instructions . len ( ) {
90
- self . tick ( part_b) ;
91
- }
91
+ ( 0 ..self . instructions . len ( ) ) . for_each ( |_| self . tick ( part_b) ) ;
92
92
}
93
93
94
94
fn tick ( & mut self , part_b : bool ) {
@@ -107,6 +107,14 @@ impl Problem {
107
107
}
108
108
}
109
109
110
+ fn score ( & self ) -> u32 {
111
+ self . board
112
+ . iter ( )
113
+ . filter ( |x| * x. 1 == Tile :: Box )
114
+ . map ( |( pos, _) | ( 100 * pos. y ( ) + pos. x ( ) ) as u32 )
115
+ . sum ( )
116
+ }
117
+
110
118
// -> was successful
111
119
fn push ( & mut self , pos : Vec2 < usize > , dir : Direction ) -> bool {
112
120
// if we are air, return true
@@ -132,6 +140,9 @@ impl Problem {
132
140
}
133
141
}
134
142
143
+ // these next two function are an absolute disaster, but im too tired to
144
+ // clean them up right now...
145
+
135
146
fn can_push ( & self , pos : Vec2 < usize > , dir : Direction ) -> bool {
136
147
// println!("{pos:?}, {dir:?}");
137
148
let value = self . board [ pos] ;
@@ -201,34 +212,6 @@ impl Problem {
201
212
false
202
213
}
203
214
}
204
-
205
- fn score ( & self ) -> u32 {
206
- let mut score = 0 ;
207
-
208
- for ( pos, _) in self . board . iter ( ) . filter ( |x| * x. 1 == Tile :: Box ) {
209
- score += ( 100 * pos. y ( ) + pos. x ( ) ) as u32 ;
210
- }
211
-
212
- score
213
- }
214
-
215
- fn debug ( & self ) {
216
- let mut dbg = self . board . clone ( ) ;
217
- dbg. set ( self . pos , Tile :: Robot ) ;
218
- println ! ( "{:?}" , dbg) ;
219
- }
220
- }
221
-
222
- impl Debug for Tile {
223
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
224
- match self {
225
- Tile :: Robot => f. write_char ( '@' ) ,
226
- Tile :: Wall => f. write_char ( '#' ) ,
227
- Tile :: Box => f. write_char ( '[' ) ,
228
- Tile :: BoxRight => f. write_char ( ']' ) ,
229
- Tile :: Empty => f. write_char ( '.' ) ,
230
- }
231
- }
232
215
}
233
216
234
217
#[ cfg( test) ]
0 commit comments