-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20.d
143 lines (120 loc) · 3.45 KB
/
day20.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
module day20;
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
void main() {
string[] input = File("input/day20.txt")
.byLine()
.map!(to!string)
.map!strip
.filter!(line => line.length > 0)
.array();
string enhancement = input[0];
assert(enhancement.length == 512);
input = input[1 .. $];
auto image = new SparseArray2d!(char)('.');
for (int j = 0; j < input.length; j++) {
string line = input[j];
for (int i = 0; i < line.length; i++) {
image[i, j] = line[i];
}
}
// image.print();
uint steps = 1;
while (steps <= 50) {
// Notice! Due to how mask is composed, color of the next image is
// "inverted". Thus the default char of image changes from dark to light
// (or vice versa).
auto nextImage = new SparseArray2d!(char)(steps % 2 == 0 ? '.' : '#');
for (int j = image.min.y - 1; j <= image.max.y + 1; j++) {
for (int i = image.min.x - 1; i <= image.max.x + 1; i++) {
Position[] area = Position(i, j).adjacentArea();
uint enhcPos = area.map!(p => image[p])
.map!(l => l == '#' ? '1' : '0')
.to!int(2);
char enhcChar = enhancement[enhcPos];
nextImage[i, j] = enhcChar;
}
}
// nextImage.print();
image = nextImage;
if (steps == 2 || steps == 50) {
int litCount = 0;
foreach (pos, val; image) {
if (val == '#') {
litCount++;
}
}
writeln(litCount);
}
steps++;
}
}
class SparseArray2d(T) {
private T[Position] arr;
private T noValue;
Position min;
Position max;
this(T noValue) {
this.noValue = noValue;
this.min = Position(0, 0);
this.max = Position(0, 0);
}
ref T opIndex(Position pos) {
return pos in arr ? arr[pos] : noValue;
}
ref T opIndex(int x, int y) {
return this[Position(x, y)];
}
void opIndexAssign(T value, Position pos) {
min.x = std.algorithm.min(min.x, pos.x);
max.x = std.algorithm.max(max.x, pos.x);
min.y = std.algorithm.min(min.y, pos.y);
max.y = std.algorithm.max(max.y, pos.y);
arr[pos] = value;
}
void opIndexAssign(T value, int x, int y) {
this[Position(x, y)] = value;
}
int opApply(int delegate(ref Position, ref T) dg) {
int result = 0;
foreach (Position key, T value; arr) {
if (dg(key, value)) {
result = 1;
break;
}
}
return result;
}
public void print() {
for (int j = min.y; j <= max.y; j++) {
for (int i = min.x; i <= max.x; i++) {
T val = arr[Position(i, j)];
write(val);
}
write("\n");
}
write("\n");
}
}
struct Position {
int x;
int y;
Position[] adjacentArea() {
return [
Position(x - 1, y - 1),
Position(x , y - 1),
Position(x + 1, y - 1),
Position(x - 1, y),
Position(x , y),
Position(x + 1, y),
Position(x - 1, y + 1),
Position(x , y + 1),
Position(x + 1, y + 1),
];
}
}