Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/advent-of-code-2021
Path: blob/main/src/day11.rs
97 views
1
const SIZE: usize = 10;
2
3
pub fn run() {
4
let mut octopi = [[0; SIZE]; SIZE];
5
for (y, line) in include_str!("inputs/day11").lines().enumerate() {
6
for (x, ch) in line.chars().enumerate() {
7
octopi[y][x] = ch.to_digit(10).unwrap();
8
}
9
}
10
11
let mut step = 0;
12
let mut total_flash_count = 0;
13
loop {
14
let mut step_flash_count = 0;
15
let mut flashes = vec![];
16
17
for y in 0..SIZE {
18
for x in 0..SIZE {
19
octopi[y][x] += 1;
20
if octopi[y][x] >= 10 {
21
flashes.push((x, y))
22
}
23
}
24
}
25
26
while let Some((x, y)) = flashes.pop() {
27
octopi[y][x] = 0;
28
step_flash_count += 1;
29
for y2 in y.saturating_sub(1)..=(y + 1).min(SIZE - 1) {
30
for x2 in x.saturating_sub(1)..=(x + 1).min(SIZE - 1) {
31
if octopi[y2][x2] == 0 {
32
// Neighbor already flashed.
33
continue;
34
}
35
octopi[y2][x2] += 1;
36
if octopi[y2][x2] >= 10 && !flashes.contains(&(x2, y2)) {
37
flashes.push((x2, y2));
38
}
39
}
40
}
41
}
42
43
step += 1;
44
total_flash_count += step_flash_count;
45
46
if step == 100 {
47
println!("{}", total_flash_count);
48
}
49
if step_flash_count == SIZE * SIZE {
50
println!("{}", step);
51
break;
52
}
53
}
54
}
55
56