-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_twelve_too_big.php
150 lines (132 loc) · 5.13 KB
/
day_twelve_too_big.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
<?php
$filePointer = fopen("input_day_twelve.txt", "r");
$originalMap = [];
$map = [];
$alphabet = range('a', 'z');
$startX = 0;
$startY = 0;
$endX = 0;
$endY = 0;
$count = 0;
while (!feof($filePointer) && $line = trim(fgets($filePointer)))
{
$splitLine = str_split($line);
$originalMap[] = str_split($line);
$numberLine = [];
foreach ($splitLine as $key => $value) {
if ($value === "S") {
$numberLine[] = array_search('a', $alphabet);
$startX = $count;
$startY = $key;
continue;
}
if ($value === "E") {
$numberLine[] = array_search('z', $alphabet);
$endX = $count;
$endY = $key;
continue;
}
$numberLine[] = array_search($value, $alphabet);
}
$map[] = $numberLine;
$count++;
}
echo "start at ($startX, $startY) end at ($endX, $endY) \n\n";
$shortestPath = 100000000;
$possiblePaths = [
new PathMap($map, $startX, $startY, [])
];
$resultPaths = [];
$count = 0;
do {
usort($possiblePaths, function ($b, $a) {
if (count($a->way) == count($b->way)) {
return 0;
}
return (count($a->way) < count($b)) ? -1 : 1;
});
foreach ($possiblePaths as $key => $pathMap) {
// $pathMap->print();
$currentPositionX = $pathMap->currentPositionX;
$currentPositionY = $pathMap->currentPositionY;
// echo "($currentPositionX, $currentPositionY)\n";
// mark position as visited
$newMap = $pathMap->map;
$newMap[$currentPositionX][$currentPositionY] = ".";
if ($currentPositionX == $endX && $currentPositionY == $endY) {
$resultPaths[] = $pathMap;
if ($shortestPath > count($pathMap->way)) {
$shortestPath = count($pathMap->way);
}
echo count($pathMap->way) . "\n";
unset($possiblePaths[$key]);
continue;
}
if (count($pathMap->way) > $shortestPath) {
unset($possiblePaths[$key]);
continue;
}
// look around
// up
if (isset($pathMap->map[$currentPositionX][$currentPositionY-1]) && $pathMap->map[$currentPositionX][$currentPositionY-1] !== '.' && abs($pathMap->map[$currentPositionX][$currentPositionY-1] - $pathMap->map[$currentPositionX][$currentPositionY]) <= 1) {
$possiblePaths[] = new PathMap($newMap, $currentPositionX, $currentPositionY-1, array_merge($pathMap->way, ["($currentPositionX, $currentPositionY)"]));
}
// down
if (isset($pathMap->map[$currentPositionX][$currentPositionY+1]) && $pathMap->map[$currentPositionX][$currentPositionY+1] !== '.' && abs($pathMap->map[$currentPositionX][$currentPositionY+1] - $pathMap->map[$currentPositionX][$currentPositionY]) <= 1) {
$possiblePaths[] = new PathMap($newMap, $currentPositionX, $currentPositionY+1, array_merge($pathMap->way, ["($currentPositionX, $currentPositionY)"]));
}
// right
// if ($currentPositionX == 3 && $currentPositionY == 0) {
// printMap($pathMap->map);
// echo $pathMap->map[$currentPositionX][$currentPositionY] . "\n";
// echo $pathMap->map[$currentPositionX+1][$currentPositionY] . "\n";
// }
if (isset($pathMap->map[$currentPositionX+1][$currentPositionY]) && $pathMap->map[$currentPositionX+1][$currentPositionY] !== '.' && abs($pathMap->map[$currentPositionX+1][$currentPositionY] - $pathMap->map[$currentPositionX][$currentPositionY]) <= 1) {
$possiblePaths[] = new PathMap($newMap, $currentPositionX+1, $currentPositionY, array_merge($pathMap->way, ["($currentPositionX, $currentPositionY)"]));
}
// left
if (isset($pathMap->map[$currentPositionX-1][$currentPositionY]) && $pathMap->map[$currentPositionX-1][$currentPositionY] !== '.' && abs($pathMap->map[$currentPositionX-1][$currentPositionY] - $pathMap->map[$currentPositionX][$currentPositionY]) <= 1) {
$possiblePaths[] = new PathMap($newMap, $currentPositionX-1, $currentPositionY, array_merge($pathMap->way, ["($currentPositionX, $currentPositionY)"]));
}
// remove old path
unset($possiblePaths[$key]);
}
$count++;
} while (count($possiblePaths) > 0);
//printMap($originalMap);
//echo "\n";
//printMap($map);
echo "\nshortest path $shortestPath\n";
function printMap($map) {
foreach ($map as $line) {
foreach ($line as $char) {
echo $char . " ";
}
echo "\n";
}
}
class PathMap {
public $map = [];
public $currentPositionX = 0;
public $currentPositionY = 0;
public $way = [];
/**
* @param array $map
* @param int $currentPositionX
* @param int $currentPositionY
* @param array $way
*/
public function __construct(array $map, int $currentPositionX, int $currentPositionY, array $way)
{
$this->map = $map;
$this->currentPositionX = $currentPositionX;
$this->currentPositionY = $currentPositionY;
$this->way = $way;
}
public function print()
{
echo "path ";
echo implode(", ", $this->way);
echo "\n";
}
}