Repository navigation
Expand file tree
/
Copy pathday10.php
More file actions
85 lines (84 loc) · 2.61 KB
/
Copy pathday10.php
File metadata and controls
85 lines (84 loc) · 2.61 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
<?php
$input = trim(file_get_contents('inputs/day10.txt'));
$map = array_map( 'trim',explode("\n", $input));
$map = [...array_map( 'str_split',$map)];
$startPositions = [];
foreach ($map as $y => $line) {
$map[$y]= array_map('intval', $line);
$startPositions[$y] = array_keys($map[$y], '0');
}
function move($y, $x) {
global $map, $visited, $nines;
if (in_array("$y,$x", $visited)) {
return;
}
$visited[] = "$y,$x";
if ($map[$y][$x] === 0 ) {
return;
}
if ($map[$y][$x] === 9) {
$nines++;
return;
}
$moveOptions = moveOptions($y, $x);
foreach ($moveOptions as $option) {
move($option['y'], $option['x']);
}
}
function move2($y, $x) {
global $map, $visited, $nines;
if ($map[$y][$x] === 0 ) {
return;
}
if ($map[$y][$x] === 9) {
$nines++;
return;
}
$moveOptions = moveOptions($y, $x);
foreach ($moveOptions as $option) {
move2($option['y'], $option['x']);
}
}
function moveOptions($y, $x) {
global $map, $visited;
$y = intval($y);
$x = intval($x);
$currentHeight = intval($map[$y][$x]);
$options = [];
if (isset($map[$y - 1][$x]) && !in_array(($y - 1).','.$x, haystack: $visited) && $map[$y - 1][$x] != 'S' && $map[$y - 1][$x] - $currentHeight === 1) {
$options[] = ['y' => $y - 1, 'x' => $x];
}
if (isset($map[$y + 1][$x]) && !in_array(($y + 1).','.$x, haystack: $visited) && $map[$y + 1][$x] != 'S' && $map[$y + 1][$x] - $currentHeight === 1) {
$options[] = ['y' => $y + 1, 'x' => $x];
}
if (isset($map[$y][$x - 1]) && !in_array($y.','.($x - 1), haystack: $visited) && $map[$y][$x - 1] != 'S' && $map[$y][$x - 1] - $currentHeight === 1) {
$options[] = ['y' => $y, 'x' => $x - 1];
}
if (isset($map[$y][$x + 1]) && !in_array($y.','.($x + 1), haystack: $visited) && $map[$y][$x + 1] != 'S' && $map[$y][$x + 1] - $currentHeight === 1) {
$options[] = ['y' => $y, 'x' => $x + 1];
}
return $options;
}
$nines = 0;
foreach ($startPositions as $y => $row) {
foreach ($row as $line => $x) {
$map[$y][$x] = 'S';
$visited = [];
move($y, $x);
$map[$y][$x] = 0;
$visited = [];
}
}
$ninesPt1 = $nines;
$nines = 0;
foreach ($startPositions as $y => $row) {
foreach ($row as $line => $x) {
$map[$y][$x] = 'S';
$visited = [];
move2($y, $x);
$map[$y][$x] = 0;
$visited = [];
}
}
echo "<p>The answer for part 1 is $ninesPt1";
echo "<p>The answer for part 2 is $nines";