-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay17.php
239 lines (202 loc) · 5.95 KB
/
Day17.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
declare(strict_types=1);
namespace AoC;
use AoC\Solution;
use AoC\Util\Map2D;
use AoC\Util\Vec2D;
use Exception;
use SplPriorityQueue;
class Day17 implements Solution
{
private Map2D $map;
private Vec2D $stop;
public function __construct(string $input)
{
$this->map = Map2D::fromInput($input);
$this->start = new Vec2D(0, 0);
$this->stop = new Vec2D(
$this->map->getW() - 1,
$this->map->getH() - 1,
);
}
public function part1(string $input): string
{
$seen = [];
$queue = new SplPriorityQueue();
foreach ([new Vec2D(1, 0), new Vec2D(0, 1)] as $next) {
$cost = (int) $this->map->getPoint($next);
$queue->insert(new Crucible(
$next,
$next,
1,
$cost,
null
), $cost);
}
while (!$queue->isEmpty()) {
/** @var Crucible $cur */
$cur = $queue->extract();
if ($cur->pos == $this->stop) {
// $this->printRoute($cur);
return (string) $cur->heatLoss;
}
foreach ($cur->possibleMoves($this->map) as $next) {
$key = $next->getKey();
if (!isset($seen[$key])) {
$from[$next->pos->getKey()] = $cur->pos;
$seen[$key] = true;
$queue->insert($next, -$next->heatLoss);
}
}
}
throw new Exception('No path found');
}
public function part2(string $input): string
{
$seen = [];
$queue = new SplPriorityQueue();
foreach ([new Vec2D(1, 0), new Vec2D(0, 1)] as $next) {
$cost = (int) $this->map->getPoint($next);
$queue->insert(new UltraCrucible(
$next,
$next,
1,
$cost,
null
), $cost);
}
while (!$queue->isEmpty()) {
/** @var UltraCrucible $cur */
$cur = $queue->extract();
if ($cur->pos == $this->stop) {
// $this->printRoute($cur);
return (string) $cur->heatLoss;
}
foreach ($cur->possibleMoves($this->map) as $next) {
$key = $next->getKey();
if (!isset($seen[$key])) {
$from[$next->pos->getKey()] = $cur->pos;
$seen[$key] = true;
$queue->insert($next, -$next->heatLoss);
}
}
}
throw new Exception('No path found');
}
public function printRoute(Crucible $crucible): void
{
$map = clone $this->map;
while ($crucible->prev !== null) {
$dir = $crucible->pos->sub($crucible->prev->pos);
$map->setPoint(
$crucible->pos,
match ($dir->getKey()) {
'1-0' => "\033[91m>\033[0m",
'-1-0' => "\033[91m<\033[0m",
'0-1' => "\033[91mv\033[0m",
'0--1' => "\033[91m^\033[0m",
default => "\033[91m#\033[0m",
}
);
$crucible = $crucible->prev;
}
echo $map . PHP_EOL;
usleep(50000);
}
}
class Crucible
{
public function __construct(
public readonly Vec2D $pos,
public readonly Vec2D $dir,
public readonly int $movesInDir,
public readonly int $heatLoss,
public readonly ?self $prev,
) { }
public function possibleMoves(Map2D $map): array
{
$dirs = [
new Vec2D(1, 0),
new Vec2D(-1, 0),
new Vec2D(0, 1),
new Vec2D(0, -1),
];
$moves = [];
foreach ($dirs as $dir) {
if ($dir == $this->dir->opposite()) {
continue;
}
if ($dir == $this->dir && $this->movesInDir === 3) {
continue;
}
$next = $this->next($dir, $map);
if ($next !== null) { $moves[] = $next; }
}
return $moves;
}
protected function next(Vec2D $dir, Map2D $map): ?self
{
$nextPos = $this->pos->add($dir);
$nextCost = $map->getPoint($nextPos);
if ($nextCost === null) {
return null;
}
return new $this(
$nextPos,
$dir,
$dir == $this->dir ? $this->movesInDir + 1 : 1,
$this->heatLoss + (int) $nextCost,
$this,
);
}
public function getKey(): string
{
return sprintf(
'%s|%s|%d',
$this->pos->getKey(),
$this->dir->getKey(),
$this->movesInDir
);
}
}
class UltraCrucible extends Crucible
{
public function possibleMoves(Map2D $map): array
{
if ($this->movesInDir < 4) {
$next = $this->next($this->dir, $map);
if ($next !== null) {
return [$next];
} else {
throw new Exception('Could not move enough in direction');
}
}
$dirs = [
new Vec2D(1, 0),
new Vec2D(-1, 0),
new Vec2D(0, 1),
new Vec2D(0, -1),
];
$moves = [];
foreach ($dirs as $dir) {
if ($dir == $this->dir->opposite()) {
continue;
}
if ($dir == $this->dir && $this->movesInDir === 10) {
continue;
}
if ($dir != $this->dir) {
$minCheckPos = $this->pos->add(new Vec2D(
$dir->x * 4,
$dir->y * 4,
));
if ($map->getPoint($minCheckPos) === null) {
continue;
}
}
$next = $this->next($dir, $map);
if ($next !== null) { $moves[] = $next; }
}
return $moves;
}
}