@@ -22,10 +22,10 @@ impl Platform {
22
22
let rows = self . tiles . len ( ) ;
23
23
let cols = self . tiles [ 0 ] . len ( ) ;
24
24
match direction {
25
- Direction :: North => self . tilt_north_south ( 0 ..= rows- 1 , |r| r+1 ..= rows- 1 ) ,
26
- Direction :: West => self . tilt_west_east ( 0 ..= cols- 1 , |c| c+1 ..= cols- 1 ) ,
27
- Direction :: South => self . tilt_north_south ( rows- 1 ..= 0 , |r| r- 1 ..= 0 ) ,
28
- Direction :: East => self . tilt_west_east ( cols- 1 ..= 0 , |c| c- 1 ..= 0 ) ,
25
+ Direction :: North => self . tilt_north_south ( 0 ..rows, |r| Box :: new ( r+1 ..rows) ) ,
26
+ Direction :: West => self . tilt_west_east ( 0 ..cols, |c| Box :: new ( c+1 ..cols) ) ,
27
+ Direction :: South => self . tilt_north_south ( ( 0 ..rows ) . rev ( ) , |r| Box :: new ( ( 0 ..r ) . rev ( ) ) ) ,
28
+ Direction :: East => self . tilt_west_east ( ( 0 ..cols ) . rev ( ) , |c| Box :: new ( ( 0 ..c ) . rev ( ) ) ) ,
29
29
}
30
30
}
31
31
@@ -36,7 +36,7 @@ impl Platform {
36
36
self . tilt ( Direction :: East ) ;
37
37
}
38
38
39
- fn tilt_north_south ( & mut self , rows : std:: ops :: RangeInclusive < usize > , source_rows : impl Fn ( usize ) -> std:: ops :: RangeInclusive < usize > ) {
39
+ fn tilt_north_south ( & mut self , rows : impl std:: iter :: Iterator < Item = usize > , source_rows : impl Fn ( usize ) -> Box < dyn std:: iter :: Iterator < Item = usize > > ) {
40
40
for r in rows {
41
41
for c in 0 ..self . tiles [ r] . len ( ) {
42
42
if self . tiles [ r] [ c] != Tile :: Empty {
@@ -64,7 +64,7 @@ impl Platform {
64
64
}
65
65
}
66
66
67
- fn tilt_west_east ( & mut self , cols : std:: ops :: RangeInclusive < usize > , source_cols : impl Fn ( usize ) -> std:: ops :: RangeInclusive < usize > ) {
67
+ fn tilt_west_east ( & mut self , cols : impl std:: iter :: Iterator < Item = usize > , source_cols : impl Fn ( usize ) -> Box < dyn std:: iter :: Iterator < Item = usize > > ) {
68
68
for c in cols {
69
69
for r in 0 ..self . tiles . len ( ) {
70
70
if self . tiles [ r] [ c] != Tile :: Empty {
@@ -229,19 +229,19 @@ O.........
229
229
#....###..
230
230
#OO..#...." ;
231
231
232
- static AFTER_SOUTH : & str = "O ....#....
232
+ static AFTER_SOUTH : & str = ". ....#....
233
233
....#....#
234
234
...O.##...
235
235
...#......
236
- . .O....O#O
236
+ O .O....O#O
237
237
O.#..O.#.#
238
238
O....#....
239
239
OO....OO..
240
240
#OO..###..
241
241
#OO.O#...O" ;
242
242
243
243
static AFTER_EAST : & str = "....O#....
244
- .O.OO #....#
244
+ .OOO #....#
245
245
.....##...
246
246
.OO#....OO
247
247
......OO#.
@@ -296,32 +296,27 @@ OO....OO..
296
296
#[ test]
297
297
fn test_tilt_north ( ) {
298
298
let mut platform: Platform = INITIAL . parse ( ) . unwrap ( ) ;
299
+ println ! ( "initial\n {}" , platform) ;
299
300
platform. tilt ( Direction :: North ) ;
301
+ println ! ( "after tilt\n {}" , platform) ;
300
302
assert_eq ! ( platform. to_string( ) . trim_end( ) , AFTER_NORTH ) ;
301
303
}
302
304
303
- #[ test]
304
- fn test_cycle ( ) {
305
- let mut platform: Platform = INITIAL . parse ( ) . unwrap ( ) ;
306
- platform. cycle ( ) ;
307
- assert_eq ! ( platform. to_string( ) . trim_end( ) , AFTER_CYCLE_1 ) ;
308
- platform. cycle ( ) ;
309
- assert_eq ! ( platform. to_string( ) . trim_end( ) , AFTER_CYCLE_2 ) ;
310
- platform. cycle ( ) ;
311
- assert_eq ! ( platform. to_string( ) . trim_end( ) , AFTER_CYCLE_3 ) ;
312
- }
313
-
314
305
#[ test]
315
306
fn test_tilt_west ( ) {
316
307
let mut platform: Platform = INITIAL . parse ( ) . unwrap ( ) ;
308
+ println ! ( "initial\n {}" , platform) ;
317
309
platform. tilt ( Direction :: West ) ;
310
+ println ! ( "after tilt\n {}" , platform) ;
318
311
assert_eq ! ( platform. to_string( ) . trim_end( ) , AFTER_WEST ) ;
319
312
}
320
313
321
314
#[ test]
322
315
fn test_tilt_south ( ) {
323
316
let mut platform: Platform = INITIAL . parse ( ) . unwrap ( ) ;
317
+ println ! ( "initial\n {}" , platform) ;
324
318
platform. tilt ( Direction :: South ) ;
319
+ println ! ( "after tilt\n {}" , platform) ;
325
320
assert_eq ! ( platform. to_string( ) . trim_end( ) , AFTER_SOUTH ) ;
326
321
}
327
322
@@ -334,6 +329,17 @@ OO....OO..
334
329
assert_eq ! ( platform. to_string( ) . trim_end( ) , AFTER_EAST ) ;
335
330
}
336
331
332
+ #[ test]
333
+ fn test_cycle ( ) {
334
+ let mut platform: Platform = INITIAL . parse ( ) . unwrap ( ) ;
335
+ platform. cycle ( ) ;
336
+ assert_eq ! ( platform. to_string( ) . trim_end( ) , AFTER_CYCLE_1 ) ;
337
+ platform. cycle ( ) ;
338
+ assert_eq ! ( platform. to_string( ) . trim_end( ) , AFTER_CYCLE_2 ) ;
339
+ platform. cycle ( ) ;
340
+ assert_eq ! ( platform. to_string( ) . trim_end( ) , AFTER_CYCLE_3 ) ;
341
+ }
342
+
337
343
#[ test]
338
344
fn test_part2 ( ) {
339
345
let mut platform: Platform = INITIAL . parse ( ) . unwrap ( ) ;
0 commit comments