-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
110 lines (92 loc) · 2.67 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::{
fmt,
io::{self, BufRead},
};
struct OctMap(Vec<Vec<u32>>);
impl OctMap {
pub fn new() -> Self {
Self(Vec::new())
}
pub fn is_valid_index(&self, x: i32, y: i32) -> bool {
let max_x = self.0.len() as i32 - 1;
let max_y = self.0[0].len() as i32 - 1;
if x < 0 || x > max_x || y < 0 || y > max_y {
return false;
}
true
}
pub fn get(&self, x: i32, y: i32) -> u32 {
if !self.is_valid_index(x, y) {
return 0;
}
self.0[x as usize][y as usize]
}
pub fn add(&mut self, x: i32, y: i32, num: u32) {
if !self.is_valid_index(x, y) {
return;
}
self.0[x as usize][y as usize] += num;
}
}
impl fmt::Display for OctMap {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut str = "".to_owned();
for x in 0..self.0.len() {
str.push_str(&self.0[x].iter().map(|x| x.to_string()).collect::<String>());
str.push_str("\n");
}
write!(f, "{}", str)
}
}
const STEPS: u32 = 100;
fn main() {
let stdin = io::stdin();
let lines = stdin.lock().lines();
let mut map = OctMap::new();
for _line in lines {
let line = _line.unwrap();
let row = line
.chars()
.map(|x| x.to_string().parse::<u32>().unwrap())
.collect::<Vec<u32>>();
map.0.push(row);
}
let max_x = map.0.len() - 1;
let max_y = map.0[0].len() - 1;
let mut flashed_cnt = 0;
for _ in 0..STEPS {
// add 1 to all oct
for x in 0..=max_x {
for y in 0..=max_y {
map.add(x as i32, y as i32, 1);
}
}
loop {
let mut flashed = false;
for x in 0..=max_x {
for y in 0..=max_y {
if map.0[x][y] > 9 {
flashed = true;
flashed_cnt += 1;
map.0[x][y] = 0;
for dx in x as i32 - 1..=x as i32 + 1 {
for dy in y as i32 - 1..=y as i32 + 1 {
if !map.is_valid_index(dx, dy) {
continue;
}
if map.get(dx, dy) == 0 {
continue;
}
map.add(dx, dy, 1);
}
}
}
}
}
if !flashed {
break;
}
}
}
println!("ans {}", flashed_cnt);
}