-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay11.rs
77 lines (67 loc) · 1.86 KB
/
Day11.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::collections::HashSet;
use std::collections::VecDeque;
use std::env;
use std::io;
type Board = Vec<Vec<u32>>;
const NEIGHBORS: [(isize, isize); 8] = [
(-1, -1),
(-1, 0),
(-1, 1),
(0, -1),
(0, 1),
(1, -1),
(1, 0),
(1, 1),
];
fn step(board: &mut Board) -> usize {
let mut queue: VecDeque<(usize, usize)> = VecDeque::new();
for y in 0..board.len() {
for x in 0..board[0].len() {
board[y][x] += 1;
if board[y][x] > 9 {
queue.push_back((y, x));
}
}
}
let mut flashed: HashSet<(usize, usize)> = HashSet::new();
while let Some(item @ (y, x)) = queue.pop_front() {
if flashed.insert(item) {
for (dy, dx) in NEIGHBORS {
// Underflow here, not relevant for the current problem
// https://glot.io/snippets/g52gjp5zhw
let np @ (ny, nx) = ((y as isize + dy) as usize, (x as isize + dx) as usize);
if let Some(v) = board.get_mut(ny).and_then(|r| r.get_mut(nx)) {
*v += 1;
if *v > 9 && !flashed.contains(&np) {
queue.push_back(np);
}
}
}
}
}
board.iter_mut().flatten().for_each(|v| {
if *v > 9 {
*v = 0
}
});
flashed.len()
}
fn main() {
let mut nums: Board = io::stdin()
.lines()
.map(|s| {
s.unwrap()
.chars()
.map(|c| c.to_digit(10).unwrap())
.collect()
})
.collect();
if env::args().nth(1).unwrap() == "1" {
println!("{}", (1..=100).map(|_| step(&mut nums)).sum::<usize>());
} else {
println!(
"{}",
(0..).skip_while(|_| step(&mut nums) != 100).next().unwrap() + 1
);
}
}