@@ -209,8 +209,8 @@ pub fn part1(input: &Input) -> u32 {
209
209
210
210
for y in 0 ..6 {
211
211
for x in 0 ..6 {
212
- let left = if x == 0 { 0 } else { total[ y] [ x - 1 ] + input. horizontal [ y] [ x - 1 ] } ;
213
- let above = if y == 0 { 0 } else { total[ y - 1 ] [ x] + input. vertical [ y - 1 ] [ x] } ;
212
+ let left = if x > 0 { total[ y] [ x - 1 ] + input. horizontal [ y] [ x - 1 ] } else { 0 } ;
213
+ let above = if y > 0 { total[ y - 1 ] [ x] + input. vertical [ y - 1 ] [ x] } else { 0 } ;
214
214
total[ y] [ x] = left. max ( above) ;
215
215
}
216
216
}
@@ -255,15 +255,17 @@ pub fn part2(input: &Input) -> u32 {
255
255
for ( ( row, gap) , steps) in current. drain ( ) {
256
256
for & ( next_row, next_gap, horizontal, vertical) in & graph[ & row] {
257
257
// Only 1 gap total is allowed, otherwise we can make a longer path.
258
- if !( gap && next_gap) {
259
- // The bit sets represent the horizonal and vertical moves from the
260
- // previous row.
261
- let extra = horizontal. biterator ( ) . map ( |x| input. horizontal [ y] [ x] ) . sum :: < u32 > ( )
262
- + vertical. biterator ( ) . map ( |x| input. vertical [ y] [ x] ) . sum :: < u32 > ( ) ;
263
-
264
- let e = next. entry ( ( next_row, gap || next_gap) ) . or_insert ( 0 ) ;
265
- * e = ( * e) . max ( steps + extra) ;
258
+ if gap && next_gap {
259
+ continue ;
266
260
}
261
+
262
+ // The bit sets represent the horizontal and vertical moves from the previous row.
263
+ let extra = horizontal. biterator ( ) . map ( |x| input. horizontal [ y] [ x] ) . sum :: < u32 > ( )
264
+ + vertical. biterator ( ) . map ( |x| input. vertical [ y] [ x] ) . sum :: < u32 > ( ) ;
265
+
266
+ // De-duplicate states so that each row has at most 76 states.
267
+ let e = next. entry ( ( next_row, gap || next_gap) ) . or_insert ( 0 ) ;
268
+ * e = ( * e) . max ( steps + extra) ;
267
269
}
268
270
}
269
271
@@ -274,7 +276,7 @@ pub fn part2(input: &Input) -> u32 {
274
276
input. extra + current[ & ( end, true ) ]
275
277
}
276
278
277
- /// Convert maze to unidrected graph.
279
+ /// Convert maze to undirected graph.
278
280
fn compress ( input : & str ) -> Graph {
279
281
let mut grid = Grid :: parse ( input) ;
280
282
let width = grid. width ;
0 commit comments