-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.php
168 lines (142 loc) · 4.08 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
<?php
class Vec2 {
public int $x;
public int $y;
function __construct(int $x = 0, int $y = 0) {
$this->x = $x;
$this->y = $y;
}
function __toString(): string {
return "($this->x, $this->y)";
}
function inBounds(int $width, int $height) {
return $this->x >= 0 && $this->x < $width && $this->y >= 0 && $this->y < $height;
}
function add(Vec2 $rhs): Vec2 {
return new Vec2($this->x + $rhs->x, $this->y + $rhs->y);
}
function sub(Vec2 $rhs): Vec2 {
return new Vec2($this->x - $rhs->x, $this->y - $rhs->y);
}
function turnLeft(): Vec2 {
return new Vec2($this->y, -$this->x);
}
function turnRight(): Vec2 {
return new Vec2(-$this->y, $this->x);
}
function arrow(): string {
if ($this == new Vec2(1, 0)) {
return '>';
} elseif ($this == new Vec2(-1, 0)) {
return '<';
} elseif ($this == new Vec2(0, 1)) {
return 'v';
} elseif ($this == new Vec2(0, -1)) {
return '^';
} else {
return '?';
}
}
}
class Node {
public Vec2 $pos;
public Vec2 $dir;
public array $path;
public int $total;
public int $straightLeft;
function __construct(Vec2 $pos, Vec2 $dir, array $path, int $total, int $straightLeft) {
$this->pos = $pos;
$this->dir = $dir;
$this->path = $path;
$this->total = $total;
$this->straightLeft = $straightLeft;
}
function visitKey(): string {
return "$this->pos{$this->dir->arrow()}$this->straightLeft";
}
function format(array $matrix): string {
$formatted = $matrix;
foreach ([...$this->path, $this] as $node) {
$pos = $node->pos;
$dir = $node->dir;
$formatted[$pos->y][$pos->x] = $dir->arrow();
}
return join(PHP_EOL, $formatted) . PHP_EOL;
}
}
function shortestPath(array $matrix, int $minStraight, int $maxStraight): Node {
$visited = [];
$queue = new \SplPriorityQueue();
foreach ([new Vec2(1, 0), new Vec2(0, 1)] as $start) {
$queue->insert(new Node($start, $start, [], intval($matrix[$start->y][$start->x]), $maxStraight - 1), 0);
}
$width = strlen($matrix[0]);
$height = count($matrix);
$dest = new Vec2($width - 1, $height - 1);
while ($queue->valid()) {
$node = $queue->extract();
if (!array_key_exists($node->visitKey(), $visited)) {
if ($node->pos == $dest) {
return $node;
}
$visited[$node->visitKey()] = true;
$dirs = [];
if ($node->straightLeft > 0) {
array_push($dirs, $node->dir);
}
if ($maxStraight - $node->straightLeft >= $minStraight) {
array_push($dirs, $node->dir->turnLeft());
array_push($dirs, $node->dir->turnRight());
}
foreach ($dirs as $dir) {
$pos = $node->pos->add($dir);
if ($pos->inBounds($width, $height)) {
$total = $node->total + intval($matrix[$pos->y][$pos->x]);
$path = [...$node->path, $node];
$straightLeft = (($dir == $node->dir) ? $node->straightLeft : $maxStraight) - 1;
$next = new Node($pos, $dir, $path, $total, $straightLeft);
$diff = $dest->sub($pos);
$cost = $total + abs($diff->x) + abs($diff->y);
if (!array_key_exists($next->visitKey(), $visited)) {
$queue->insert($next, -$cost);
}
}
}
}
}
throw new Exception("Destination not found!");
}
function printUsage() {
echo "Usage: day17 [--dump] <path to input>" . PHP_EOL;
}
$filePath = null;
$dump = false;
foreach (array_slice($argv, 1) as $arg) {
if ($arg == '--dump') {
$dump = true;
} else if ($arg == '--help') {
printUsage();
exit(0);
} else if (preg_match('/--\w+/', $arg)) {
echo "Unrecognized argument: $arg" . PHP_EOL;
exit(1);
} else {
$filePath = $arg;
}
}
if ($filePath == null) {
printUsage();
exit(1);
}
$raw = trim(file_get_contents($filePath));
$input = preg_split('/\R/', $raw);
$part1Node = shortestPath($input, 1, 3);
if ($dump) {
echo $part1Node->format($input);
}
echo "Part 1: $part1Node->total" . PHP_EOL;
$part2Node = shortestPath($input, 4, 10);
if ($dump) {
echo $part2Node->format($input);
}
echo "Part 2: $part2Node->total" . PHP_EOL;