Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/advent-of-code-2021
Path: blob/main/src/day25.rs
97 views
1
pub fn run() {
2
let mut sea_floor: Vec<Vec<_>> = include_str!("inputs/day25")
3
.lines()
4
.map(|l| {
5
l.chars()
6
.map(|ch| match ch {
7
'.' => Tile::Empty,
8
'>' => Tile::EastCucumber,
9
'v' => Tile::SouthCucumber,
10
_ => unreachable!(),
11
})
12
.collect()
13
})
14
.collect();
15
16
let mut steps = 1;
17
while step(&mut sea_floor) {
18
steps += 1;
19
}
20
println!("{}", steps)
21
}
22
23
#[derive(Eq, PartialEq, Clone)]
24
enum Tile {
25
Empty,
26
EastCucumber,
27
SouthCucumber,
28
}
29
30
fn step(sea_floor: &mut Vec<Vec<Tile>>) -> bool {
31
let height = sea_floor.len();
32
let width = sea_floor[0].len();
33
34
let mut east_moves = vec![];
35
for y in 0..height {
36
for x in 0..width {
37
let east_x = (x + 1) % width;
38
if sea_floor[y][x] == Tile::EastCucumber && sea_floor[y][east_x] == Tile::Empty {
39
east_moves.push((x, y, east_x))
40
}
41
}
42
}
43
44
for &(x, y, east_x) in east_moves.iter() {
45
sea_floor[y][x] = Tile::Empty;
46
sea_floor[y][east_x] = Tile::EastCucumber;
47
}
48
49
let mut south_moves = vec![];
50
for x in 0..width {
51
for y in 0..height {
52
let south_y = (y + 1) % height;
53
if sea_floor[y][x] == Tile::SouthCucumber && sea_floor[south_y][x] == Tile::Empty {
54
south_moves.push((x, y, south_y));
55
}
56
}
57
}
58
59
for &(x, y, south_y) in south_moves.iter() {
60
sea_floor[y][x] = Tile::Empty;
61
sea_floor[south_y][x] = Tile::SouthCucumber;
62
}
63
64
east_moves.len() + south_moves.len() > 0
65
}
66
67