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
|
use std::io;
fn valid(map: &[Vec<char>], x: i32, y: i32) -> bool {
if x < 0 {
return false;
}
if y < 0 {
return false;
}
let uy: usize = y.try_into().unwrap();
if uy >= map.len() {
return false;
}
let ux: usize = x.try_into().unwrap();
if ux >= map[uy].len() {
return false;
}
true
}
fn is_paper(map: &[Vec<char>], x: i32, y: i32) -> bool {
if valid(map, x, y) {
let uy: usize = y.try_into().unwrap();
let ux: usize = x.try_into().unwrap();
return map[uy][ux] == '@';
}
false
}
fn adjacent(map: &[Vec<char>], x: i32, y: i32) -> usize {
let mut count = 0;
for dx in -1..=1 {
if is_paper(map, x + dx, y - 1) {
count += 1;
}
if is_paper(map, x + dx, y + 1) {
count += 1;
}
}
if is_paper(map, x - 1, y) {
count += 1;
}
if is_paper(map, x + 1, y) {
count += 1;
}
count
}
fn main() {
let mut map: Vec<Vec<char>> = Vec::new();
loop {
let mut buffer = String::new();
let bytes = io::stdin().read_line(&mut buffer).unwrap();
if bytes == 0 {
break;
}
map.push(buffer.trim_end().chars().collect());
}
let mut total = 0;
for y in 0..map.len() {
for x in 0..map[y].len() {
if is_paper(&map, x.try_into().unwrap(), y.try_into().unwrap())
&& adjacent(&map, x.try_into().unwrap(), y.try_into().unwrap()) < 4
{
total += 1;
}
}
}
println!("{}", total);
let mut total2 = 0;
loop {
total = 0;
let mut next = map.clone();
for y in 0..map.len() {
for x in 0..map[y].len() {
if is_paper(&map, x.try_into().unwrap(), y.try_into().unwrap())
&& adjacent(&map, x.try_into().unwrap(), y.try_into().unwrap()) < 4
{
total += 1;
next[y][x] = 'x';
}
}
}
if total == 0 {
break;
}
map = next;
total2 += total;
}
println!("{}", total2);
}
|