-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.php
162 lines (133 loc) · 4.85 KB
/
Day12.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
<?php
declare(strict_types=1);
namespace App\Days;
use App\Contracts\Day;
use Illuminate\Support\Collection;
use SplQueue;
class Day12 extends Day
{
public const EXAMPLE1 = <<<eof
Sabqponm
abcryxxl
accszExk
acctuvwj
abdefghi
eof;
private array $directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]; // up, down, left, right
/**
* Solve Part 1 of the day's problem.
*/
public function solvePart1(mixed $input): int|string|null
{
$grid = $this->parseInput($input)->toArray();
$rows = count($grid);
$cols = count($grid[0]);
// Find start and end positions
$start = null;
$end = null;
foreach ($grid as $y => $row) {
foreach ($row as $x => $cell) {
match ($cell) {
'S' => [$start, $grid[$y][$x]] = [[$y, $x], 'a'],
'E' => [$end, $grid[$y][$x]] = [[$y, $x], 'z'],
default => null,
};
}
}
// keep phpstan happy
if (null === $start || null === $end) {
return null;
}
// Breadth-First Search
$queue = new SplQueue();
$queue->enqueue([$start, 0]); // [position, steps]
$visited = array_fill(0, $rows, array_fill(0, $cols, false));
$visited[$start[0]][$start[1]] = true;
while (!$queue->isEmpty()) {
[$current, $steps] = $queue->dequeue();
[$x, $y] = $current;
if ($current === $end) {
return $steps; // Found the shortest path
}
foreach ($this->directions as [$dx, $dy]) {
$newX = (int) $x + $dx;
$newY = (int) $y + $dy;
// check if the new position is within the grid bounds
// check if the new position has not been visited
// check if the new position is at most one level higher than the current position
if ($newX >= 0 && $newX < $rows && $newY >= 0 && $newY < $cols
&& !$visited[$newX][$newY]
&& ord($grid[$newX][$newY]) <= ord($grid[$x][$y]) + 1
) {
$queue->enqueue([[$newX, $newY], $steps + 1]);
$visited[$newX][$newY] = true;
}
}
}
return null; // No path found
}
/**
* Solve Part 2 of the day's problem.
*/
public function solvePart2(mixed $input): int|string|null
{
$grid = $this->parseInput($input)->toArray();
$rows = count($grid);
$cols = count($grid[0]);
// Find start and end positions
$startingPoints = [];
$end = null;
foreach ($grid as $y => $row) {
foreach ($row as $x => $cell) {
match ($cell) {
'S' => [$startingPoints[], $grid[$y][$x]] = [[$y, $x], 'a'],
'E' => [$end, $grid[$y][$x]] = [[$y, $x], 'z'],
'a' => $startingPoints[] = [$y, $x],
default => null,
};
}
}
return null !== $end
? $this->bfs($grid, $startingPoints, $end, $rows, $cols)
: null;
}
protected function bfs(array $grid, array $startingPoints, array $end, int $rows, int $cols): int|string|null
{
$queue = new SplQueue();
foreach ($startingPoints as $point) {
$queue->enqueue([$point, 0]); // [position, steps]
}
$visited = array_fill(0, $rows, array_fill(0, $cols, false));
foreach ($startingPoints as $point) {
$visited[$point[0]][$point[1]] = true;
}
while (!$queue->isEmpty()) {
[$current, $steps] = $queue->dequeue();
[$x, $y] = $current;
if ($current === $end) {
return $steps; // found the shortest path
}
foreach ($this->directions as [$dx, $dy]) {
$newX = $x + $dx;
$newY = $y + $dy;
if (is_int($newX) && is_int($newY) && $newX >= 0 && $newX < $rows && $newY >= 0 && $newY < $cols && !$visited[$newX][$newY] && ord($grid[$newX][$newY]) <= ord($grid[$x][$y]) + 1
) {
$queue->enqueue([[$newX, $newY], $steps + 1]);
$visited[$newX][$newY] = true;
}
}
}
return null; // no path found
}
/**
* Parse the input data.
*/
protected function parseInput(mixed $input): Collection
{
$input = is_array($input) ? $input : explode("\n", $input);
return collect($input)
// convert each line into a 2D array
->map(fn (string $line): array => mb_str_split($line))
;
}
}