-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.d
170 lines (138 loc) · 4.05 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
module day11;
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
void main() {
uint[][] input = File("input/day11.txt")
.byLine()
.map!(to!string)
.map!strip
.filter!(line => line.length > 0)
.map!(line => line.map!(c => c.to!string
.to!uint).array())
.array();
auto octi = new Array2d!(Dumbo)(input.join().map!((i) { return new Dumbo(i); }).array(), input[0].length);
// octi.print();
int step = 0;
uint totalFlashesCount = 0;
while (true) {
auto nextOcti = new Array2d!(Dumbo)(octi.arr.map!((d) { return new Dumbo(d); }).array(), octi.width);
foreach (x; 0..nextOcti.width) {
foreach (y; 0..nextOcti.height) {
nextOcti[x, y].energy += 1;
}
}
bool anyDumbosFlashed = true;
while (anyDumbosFlashed == true) {
anyDumbosFlashed = false;
foreach (x; 0 .. nextOcti.width) {
foreach (y; 0 .. nextOcti.height) {
Dumbo dumbo = nextOcti[x, y];
if (dumbo.energy >= 10 && dumbo.flashed == false) {
anyDumbosFlashed = true;
dumbo.flashed = true;
totalFlashesCount++;
dumbo.energy = 0;
foreach (adj; Position(x, y).adjacent(nextOcti.width - 1, nextOcti.height - 1)) {
Dumbo adjDumbo = nextOcti[adj.x, adj.y];
if (!adjDumbo.flashed) {
adjDumbo.energy += 1;
}
}
}
}
}
}
octi = nextOcti;
step++;
// pt 1
if (step == 100) {
writeln(totalFlashesCount, " flashes after ", step, " steps.");
}
// pt 2
bool allFlashed = true;
foreach(o; nextOcti) {
if (o.flashed == false) {
allFlashed = false;
break;
}
}
if (allFlashed) {
writeln("All octopuses flash in step ", step, ".");
break;
}
// writeln("After step ", step, ": ");
// nextOcti.print();
}
}
struct Position {
uint x;
uint y;
public Position[] adjacent(int boundX, int boundY) {
Position[] adjs;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (i == 0 && j == 0) {
continue;
}
int nx = this.x + i;
int ny = this.y + j;
if (nx >= 0 && nx <= boundX && ny >= 0 && ny <= boundY) {
adjs ~= [Position(nx, ny)];
}
}
}
return adjs;
}
}
class Dumbo {
private int energy;
public bool flashed;
public this(int energy) {
this.energy = energy;
}
public this(Dumbo d) {
this.energy = d.energy;
}
override string toString() const {
// return energy.to!string ~ "" ~ (flashed ? "T" : "F") ~ " ";
return energy == 0 ? "\x1b[1m" ~ energy.to!string ~ "\x1b[0m" : energy.to!string;
}
}
class Array2d(T) {
public T[] arr;
private uint width;
private uint height;
public this(T[] arr, uint width) {
this.arr = arr;
this.width = width;
this.height = arr.length / width;
}
ref T opIndex(uint x, uint y) {
return arr[x + y * width];
}
int opApply(int delegate(ref T) dg) {
int result = 0;
foreach (T item; arr) {
if (dg(item)) {
result = 1;
break;
}
}
return result;
}
public void print() {
for (int i = 0; i < arr.length; i++) {
auto a = arr[i];
write(a);
if (i % width == width - 1) {
write("\n");
}
}
write("\n");
}
}