-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.d
107 lines (95 loc) · 2.2 KB
/
day11.d
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
import std.array, std.algorithm, std.stdio, std.file, std.conv;
alias grid = int[10][10];
int[10] parseLine(char[] line) {
int[10] output;
foreach(i, chr; line) {
output[i] = to!int(to!string(chr));
}
return output;
}
grid pop(long r, long c, grid data) {
foreach (row; r-1 .. r+2) {
if (row < 0) continue;
if (row > 9) continue;
foreach (col; c-1 .. c+2) {
if (col < 0) continue;
if (col > 9) continue;
if (data[row][col] != 0) {
data[row][col] += 1;
}
}
}
data[r][c] = 0;
return data;
}
grid increase(grid data) {
foreach (i, row; data) {
data[i] = row[] += 1;
}
return data;
}
struct Point {
long row;
long col;
}
struct PointList {
int length;
Point[100] points;
void add(long row, long col) {
this.points[length++] = Point(row, col);
}
}
PointList findNextPops(grid data) {
PointList result;
foreach (r, row; data) {
foreach (c, num; row) {
if (num > 9) {
result.add(r, c);
}
}
}
return result;
}
bool allZero(grid data) {
foreach (row; data) {
foreach (pos; row) {
if (pos != 0) return false;
}
}
return true;
}
struct StepPair {
grid data;
int flashes;
}
StepPair doStep(grid data) {
auto next = increase(data);
auto popList = findNextPops(next);
int flashes;
while (popList.length > 0) {
foreach (i; 0 .. popList.length) {
auto point = popList.points[i];
next = pop(point.row, point.col, next);
}
flashes += popList.length;
popList = findNextPops(next);
}
return StepPair(next, flashes);
}
void main()
{
auto file = File("day11.in");
scope(exit) file.close();
grid data = file.byLine().map!(parseLine).array;
StepPair next = doStep(data);
int totalFlashes = next.flashes;
foreach (i; 1..300) {
next = doStep(next.data);
totalFlashes += next.flashes;
if (allZero(next.data)) {
writeln("Part2: ", i+1);
break;
}
}
writeln("Part1: ", totalFlashes);
}