blob: 666eea756bddf78c06c873b02c66551800112d65 (
plain) (
blame)
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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Grid {
int e[10][10];
};
static int step(struct Grid *next, struct Grid grid) {
for (int y = 0; y < 10; ++y)
for (int x = 0; x < 10; ++x) {
next->e[y][x] = grid.e[y][x] + 1;
}
int flashes = 0;
for (;;) {
int f = 0;
for (int y = 0; y < 10; ++y)
for (int x = 0; x < 10; ++x) {
if (next->e[y][x] < 10) continue;
f++;
next->e[y][x] = 0;
for (int dy = -1; dy < 2; ++dy)
for (int dx = -1; dx < 2; ++dx) {
if (y+dy < 0 || y+dy > 9) continue;
if (x+dx < 0 || x+dx > 9) continue;
if (!next->e[y+dy][x+dx]) continue;
next->e[y+dy][x+dx]++;
}
}
flashes += f;
if (!f) break;
}
return flashes;
}
int main(void) {
size_t cap = 0;
char *buf = NULL;
struct Grid grid = {0};
for (int y = 0; 0 < getline(&buf, &cap, stdin); ++y) {
for (int x = 0; x < 10; ++x) {
grid.e[y][x] = buf[x] - '0';
}
}
int flashes = 0;
for (int i = 0; i < 100; ++i) {
flashes += step(&grid, grid);
}
printf("%d\n", flashes);
struct Grid zero = {0};
for (int i = 100;; ++i) {
step(&grid, grid);
if (memcmp(&grid, &zero, sizeof(grid))) continue;
printf("%d\n", i+1);
break;
}
}
|