Repository navigation
Expand file tree
/
Copy pathday20.php
More file actions
114 lines (104 loc) · 3.38 KB
/
Copy pathday20.php
File metadata and controls
114 lines (104 loc) · 3.38 KB
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
<?php
$input = file_get_contents('inputs/day20.txt');
$input = preg_split('/\r?\n/', $input);
$map = [];
foreach ($input as $keyInput => $line) {
$map[] = str_split($line);
}
foreach ($map as $y => $line) {
foreach ($line as $x => $cell) {
if ($cell == 'S') {
$startPosition = "$y,$x";
}
if ($cell == 'E') {
$endPosition = "$y,$x";
}
}
}
$directions = [
['x' => 0, 'y' => -1],
['x' => 1, 'y' => 0],
['x' => 0, 'y' => 1],
['x' => -1, 'y' => 0]
];
$shortestPath = 0;
function findPathDay20($position, $score) {
global $map, $visited, $frontiers, $directions,$endPosition, $shortestPath;
[$y, $x] = explode(',', $position);
$visited[$position] = $score;
if ($endPosition == "$y,$x") {
$shortestPath = $score;
return true;
}
foreach ($directions as $direction) {
$newX = $x + $direction['x'];
$newY = $y + $direction['y'];
$newPosition = "$newY,$newX";
if (!isset($map[$newY][$newX])) {
continue;
}
if ($map[$newY][$newX] == '#') {
continue;
}
if (!isset($map[$newY][$newX]) || $map[$newY][$newX] == '#' || isset($visited[$newPosition])) {
continue;
}
$frontiers[$newPosition] = $score + 1;
}
}
$frontiers[$startPosition] = 0;
$visited = [];
while ($frontiers) {
asort($frontiers);
$frontierPos = array_key_first($frontiers);
$score = array_shift($frontiers);
if(findPathDay20($frontierPos, $score)) {
break;
}
}
$cheatStartEnd = [];
$shortcuts = [];
foreach ($visited as $keyInput => $value) {
[$y, $x] = explode(',', $keyInput);
foreach ($directions as $direction) {
$newX = $x + ($direction['x'] << 1);
$newY = $y + ($direction['y'] << 1);
$newPosition = "$newY,$newX";
if (!isset($visited[$newPosition])) {
continue;
}
if ($value + 2 + 99 < $visited[$newPosition]) {
$shortcuts[] = $visited[$newPosition] - $value - 2;
}
}
}
echo "<p>The answer for part 1 is: ". count($shortcuts);
function findCoordinatesWithinManhattanDistance($y_center, $x_center) {
global $visited;
$max_distance = 20;
$coordinates = [];
for ($y = $y_center - $max_distance; $y <= $y_center + $max_distance; $y++) {
for ($x = $x_center - $max_distance; $x <= $x_center + $max_distance; $x++) {
if (abs($y_center - $y) + abs($x_center - $x) <= $max_distance && isset($visited["$y,$x"])) {
$coordinates[] = [$y, $x];
}
}
}
return $coordinates;
}
$shortcuts = [];
foreach ($visited as $keyInput => $value) {
[$y, $x] = explode(',', $keyInput);
for ($ym = $y - 20; $ym <= $y + 20; $ym++) {
for ($xm = $x - 20; $xm <= $x + 20; $xm++) {
if (!isset($visited["$ym,$xm"])) {
continue;
}
$manhattanDistance = abs($y - $ym) + abs($x - $xm);
if ($manhattanDistance <= 20 && $value + $manhattanDistance < $visited["$ym,$xm"] && $visited["$ym,$xm"] - $value - $manhattanDistance > 99) {
$shortcuts[] = $visited["$ym,$xm"] - $value - $manhattanDistance;
}
}
}
}
echo "<p>The answer for part 2 is: ". count($shortcuts);