-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd11.java
95 lines (88 loc) · 3.06 KB
/
d11.java
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import static java.lang.System.out;
public class d11 {
public static void main(String[] args) {
var input = new BufferedReader(new InputStreamReader(System.in));
var board = new Board(input);
int result = 0, s = 1, all = 0;
while (s <= 100) {
var flashes = board.step();
result += flashes;
if (flashes == board.size * board.size && all == 0) {
all = s;
}
s++;
}
out.println(result);
if (all > 0) {
out.println(all);
} else {
while (board.step() < board.size * board.size) s++;
out.println(s);
}
}
static class Board {
static final int CAPACITY = 1000;
final int[] energy;
final int size;
Board(BufferedReader input) {
this.energy = new int[CAPACITY * CAPACITY];
Arrays.fill(this.energy, -1000);
int i = 1;
for (String line : input.lines().toList()) {
for (int j = 1; j <= line.length(); j++) {
this.energy[i * CAPACITY + j] = line.charAt(j - 1) - '0';
}
i++;
}
this.size = i - 1;
}
void print() {
for (int i = 1; i <= this.size; i++) {
for (int j = 1; j <= this.size; j++) {
out.print(" " + this.energy[i * CAPACITY + j]);
}
out.println();
}
}
int step() {
for (int i = 1; i <= this.size; i++) {
for (int j = 1; j <= this.size; j++) {
this.energy[i * CAPACITY + j]++;
}
}
int i = 1, flashes = 0;
while (i <= this.size) {
int j = 1;
while (j <= this.size) {
if (this.energy[i * CAPACITY + j] > 9) {
this.energy[(i - 1) * CAPACITY + j]++;
this.energy[(i + 1) * CAPACITY + j]++;
this.energy[(i - 1) * CAPACITY + j - 1]++;
this.energy[(i + 1) * CAPACITY + j + 1]++;
this.energy[i * CAPACITY + j - 1]++;
this.energy[i * CAPACITY + j + 1]++;
this.energy[(i - 1) * CAPACITY + j + 1]++;
this.energy[(i + 1) * CAPACITY + j - 1]++;
this.energy[i * CAPACITY + j] = -100;
i = 0;
flashes++;
break;
}
j++;
}
i++;
}
for (i = 1; i <= this.size; i++) {
for (int j = 1; j <= this.size; j++) {
if (this.energy[i * CAPACITY + j] < 0) {
this.energy[i * CAPACITY + j] = 0;
}
}
}
return flashes;
}
}
}