-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoctopus.c
87 lines (70 loc) · 2.31 KB
/
octopus.c
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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 32
int try_flash(int *map, int row, int col, int rows, int cols) {
if (row < 0 || col < 0 || row >= rows || col >= cols) return 0;
if (map[row * SIZE + col] > 9) return 0;
// don't bother incrementing already flashed
map[row * SIZE + col]++;
// leave if already flashed
if (map[row * SIZE + col] != 10) return 0;
int nflashes = 1;
nflashes += try_flash(map, row-1, col, rows, cols);
nflashes += try_flash(map, row+1, col, rows, cols);
nflashes += try_flash(map, row, col-1, rows, cols);
nflashes += try_flash(map, row, col+1, rows, cols);
nflashes += try_flash(map, row-1, col-1, rows, cols);
nflashes += try_flash(map, row+1, col+1, rows, cols);
nflashes += try_flash(map, row+1, col-1, rows, cols);
nflashes += try_flash(map, row-1, col+1, rows, cols);
return nflashes;
}
void print_map(int *map, int rows, int cols) {
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
printf("%d ", map[row * SIZE + col]);
}
printf("\n");
}
printf("\n");
}
int main(int argc, char **argv) {
int map[SIZE*SIZE];
int rows = 0, cols = 0;
char buf[32];
while (fgets(buf, 32, stdin) != NULL) {
cols = 0;
while (1) {
if (buf[cols] > '9' || buf[cols] < '0') break;
map[rows * SIZE + cols] = buf[cols] - '0';
cols++;
}
rows++;
}
int nflashes = 0;
for (int step = 0; step < 1000; step++) {
printf(".");
int nflashes2 = 0;
//print_map(map, rows, cols);
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
nflashes2 += try_flash(map, row, col, rows, cols);
}
}
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (map[row * SIZE + col] > 9) map[row * SIZE + col] = 0;
}
}
nflashes += nflashes2;
if (step == 99) {
printf("\npart 1 nflashes = %d\n", nflashes);
}
if (nflashes2 == rows * cols) {
printf("\npart 2 at round %d, they were all synchronized\n", step + 1);
break;
}
}
}