-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path15.php
77 lines (61 loc) · 1.65 KB
/
15.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
<?php
$time_start = microtime(true);
$input = trim(file_get_contents('15.txt'));
[$grid, $moves] = explode("\n\n", $input);
$grid = explode("\n", $grid);
$moves = str_split($moves);
// find robot position in grid
foreach ($grid as $y => $line) {
$x = strpos($line, '@');
if ($x !== false) {
$ry = $y;
$rx = $x;
break;
}
}
// attempt to perform all the moves
$directions = [
'^' => [-1, 0],
'>' => [0, 1],
'v' => [1, 0],
'<' => [0, -1],
];
foreach ($moves as $i => $m) {
if ($m === "\n") continue;
// echo "Move $m\n";
// step to first free spot in direction
$y2 = $ry;
$x2 = $rx;
do {
$y2 += $directions[$m][0];
$x2 += $directions[$m][1];
} while ($y2 >= 0 && $x2 >= 0 && $y2 < count($grid) && $x2 < strlen($grid[$y2]) && $grid[$y2][$x2] === 'O');
// if we're still inside grid and at a free spot, we can perform the move
if ($y2 >= 0 && $x2 >= 0 && $y2 < count($grid) && $x2 < strlen($grid[$y2]) && $grid[$y2][$x2] === '.') {
// shift everything up to robot in direction
do {
$y3 = $y2 + $directions[$m][0] * -1;
$x3 = $x2 + $directions[$m][1] * -1;
$grid[$y2][$x2] = $grid[$y3][$x3];
$y2 = $y3;
$x2 = $x3;
} while ($grid[$y2][$x2] !== '@');
// robot leaves behind a free spot
$grid[$ry][$rx] = '.';
$ry += $directions[$m][0];
$rx += $directions[$m][1];
assert($grid[$ry][$rx] === '@');
}
}
$pt1 = 0;
foreach ($grid as $r => $row) {
for ($c = 0; $c < strlen($row); $c++) {
if ($row[$c] === 'O') $pt1 += ($r * 100) + $c;
}
}
$pt2 = 0;
echo "--- Day 15 ---", PHP_EOL;
echo "Part 1: ", $pt1, PHP_EOL;
echo "Part 2: ", $pt2, PHP_EOL;
echo "Took ", (microtime(true) - $time_start) * 1000, " ms", PHP_EOL;
echo PHP_EOL;