-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-24.php
178 lines (139 loc) · 5.43 KB
/
day-24.php
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
171
172
173
174
175
176
177
178
<?php
$input = trim(file_get_contents(__DIR__ . '/input/day-24'));
$start = microtime(true);
$input = explode("\n", $input);
$input = array_map(fn($row) => str_split($row), $input);
$originalInput = $input;
$minutes = 0;
$mem = [];
while (true) {
$minutes++;
$newInput = [];
foreach ($input as $y => $line) {
foreach ($line as $x => $item) {
$num = 0;
if (($input[$y][$x - 1] ?? '.') === '#') $num++;
if (($input[$y + 1][$x] ?? '.') === '#') $num++;
if (($input[$y][$x + 1] ?? '.') === '#') $num++;
if (($input[$y - 1][$x] ?? '.') === '#') $num++;
if ($item === '#' && $num != 1) {
$newInput[$y][$x] = '.';
continue;
}
if ($item === '.' && ($num === 1 || $num === 2)) {
$newInput[$y][$x] = '#';
continue;
}
$newInput[$y][$x] = $item;
}
}
$input = $newInput;
$compressed = serialize($input);
if (isset($mem[$compressed])) {
$sum = 0;
foreach ($input as $y => $line) {
foreach ($line as $x => $item) {
if ($item === '#') {
$sum += 2 ** ($y * 5 + $x);
}
}
}
echo 'Part 1: ' . $sum . PHP_EOL;
break;
}
$mem[$compressed] = $minutes;
}
$input = $originalInput;
$minutes = 0;
$input = [0 => $input];
$minLevel = 0;
$maxLevel = 0;
while (true) {
$minutes++;
$newInput = [];
for ($level = $minLevel - 1; $level <= $maxLevel + 1; $level++) {
$nextLevel = $level + 1;
$prevLevel = $level - 1;
for ($y = 0; $y < 5; $y++) {
for ($x = 0; $x < 5; $x++) {
$item = $input[$level][$y][$x] ?? '.';
if ($y === 2 && $x === 2) continue;
$num = 0;
if ($y === 1 && $x === 2) {
if (($input[$nextLevel][0][0] ?? '.') === '#') $num++;
if (($input[$nextLevel][0][1] ?? '.') === '#') $num++;
if (($input[$nextLevel][0][2] ?? '.') === '#') $num++;
if (($input[$nextLevel][0][3] ?? '.') === '#') $num++;
if (($input[$nextLevel][0][4] ?? '.') === '#') $num++;
}
if ($y === 2 && $x === 1) {
if (($input[$nextLevel][0][0] ?? '.') === '#') $num++;
if (($input[$nextLevel][1][0] ?? '.') === '#') $num++;
if (($input[$nextLevel][2][0] ?? '.') === '#') $num++;
if (($input[$nextLevel][3][0] ?? '.') === '#') $num++;
if (($input[$nextLevel][4][0] ?? '.') === '#') $num++;
}
if ($y === 2 && $x === 3) {
if (($input[$nextLevel][0][4] ?? '.') === '#') $num++;
if (($input[$nextLevel][1][4] ?? '.') === '#') $num++;
if (($input[$nextLevel][2][4] ?? '.') === '#') $num++;
if (($input[$nextLevel][3][4] ?? '.') === '#') $num++;
if (($input[$nextLevel][4][4] ?? '.') === '#') $num++;
}
if ($y === 3 && $x === 2) {
if (($input[$nextLevel][4][0] ?? '.') === '#') $num++;
if (($input[$nextLevel][4][1] ?? '.') === '#') $num++;
if (($input[$nextLevel][4][2] ?? '.') === '#') $num++;
if (($input[$nextLevel][4][3] ?? '.') === '#') $num++;
if (($input[$nextLevel][4][4] ?? '.') === '#') $num++;
}
if ($y === 0) {
if (($input[$prevLevel][1][2] ?? '.') === '#') $num++;
}
if ($x === 0) {
if (($input[$prevLevel][2][1] ?? '.') === '#') $num++;
}
if ($x === 4) {
if (($input[$prevLevel][2][3] ?? '.') === '#') $num++;
}
if ($y === 4) {
if (($input[$prevLevel][3][2] ?? '.') === '#') $num++;
}
if (($input[$level][$y][$x - 1] ?? '.') === '#') $num++;
if (($input[$level][$y + 1][$x] ?? '.') === '#') $num++;
if (($input[$level][$y][$x + 1] ?? '.') === '#') $num++;
if (($input[$level][$y - 1][$x] ?? '.') === '#') $num++;
if ($item === '#' && $num != 1) {
$newInput[$level][$y][$x] = '.';
continue;
}
if ($item === '.' && ($num === 1 || $num === 2)) {
$newInput[$level][$y][$x] = '#';
if ($minLevel === $level) {
$minLevel--;
}
if ($maxLevel === $level) {
$maxLevel++;
}
continue;
}
$newInput[$level][$y][$x] = $item;
}
}
}
$input = $newInput;
if ($minutes === 200) {
$bugs = 0;
for ($level = $minLevel; $level <= $maxLevel; $level++) {
for ($y = 0; $y < 5; $y++) {
for ($x = 0; $x < 5; $x++) {
$item = $input[$level][$y][$x] ?? '.';
if ($item === '#') $bugs++;
}
}
}
echo 'Part 2: ' . $bugs . PHP_EOL;
break;
}
}
echo 'Finished in ' . (microtime(true) - $start) . PHP_EOL;