Skip to content

Commit 4cc76e5

Browse files
committed
Replace ranges with explicit iterators
This fixes the separate tilt methods to produce the correct results (after fixing up the expected typos in the test data). The cycle test now loops forever ...
1 parent f65527b commit 4cc76e5

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/day14.rs

+26-20
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ impl Platform {
2222
let rows = self.tiles.len();
2323
let cols = self.tiles[0].len();
2424
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())),
2929
}
3030
}
3131

@@ -36,7 +36,7 @@ impl Platform {
3636
self.tilt(Direction::East);
3737
}
3838

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>>) {
4040
for r in rows {
4141
for c in 0..self.tiles[r].len() {
4242
if self.tiles[r][c] != Tile::Empty {
@@ -64,7 +64,7 @@ impl Platform {
6464
}
6565
}
6666

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>>) {
6868
for c in cols {
6969
for r in 0..self.tiles.len() {
7070
if self.tiles[r][c] != Tile::Empty {
@@ -229,19 +229,19 @@ O.........
229229
#....###..
230230
#OO..#....";
231231

232-
static AFTER_SOUTH: &str = "O....#....
232+
static AFTER_SOUTH: &str = ".....#....
233233
....#....#
234234
...O.##...
235235
...#......
236-
..O....O#O
236+
O.O....O#O
237237
O.#..O.#.#
238238
O....#....
239239
OO....OO..
240240
#OO..###..
241241
#OO.O#...O";
242242

243243
static AFTER_EAST: &str = "....O#....
244-
.O.OO#....#
244+
.OOO#....#
245245
.....##...
246246
.OO#....OO
247247
......OO#.
@@ -296,32 +296,27 @@ OO....OO..
296296
#[test]
297297
fn test_tilt_north() {
298298
let mut platform: Platform = INITIAL.parse().unwrap();
299+
println!("initial\n{}", platform);
299300
platform.tilt(Direction::North);
301+
println!("after tilt\n{}", platform);
300302
assert_eq!(platform.to_string().trim_end(), AFTER_NORTH);
301303
}
302304

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-
314305
#[test]
315306
fn test_tilt_west() {
316307
let mut platform: Platform = INITIAL.parse().unwrap();
308+
println!("initial\n{}", platform);
317309
platform.tilt(Direction::West);
310+
println!("after tilt\n{}", platform);
318311
assert_eq!(platform.to_string().trim_end(), AFTER_WEST);
319312
}
320313

321314
#[test]
322315
fn test_tilt_south() {
323316
let mut platform: Platform = INITIAL.parse().unwrap();
317+
println!("initial\n{}", platform);
324318
platform.tilt(Direction::South);
319+
println!("after tilt\n{}", platform);
325320
assert_eq!(platform.to_string().trim_end(), AFTER_SOUTH);
326321
}
327322

@@ -334,6 +329,17 @@ OO....OO..
334329
assert_eq!(platform.to_string().trim_end(), AFTER_EAST);
335330
}
336331

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+
337343
#[test]
338344
fn test_part2() {
339345
let mut platform: Platform = INITIAL.parse().unwrap();

0 commit comments

Comments
 (0)