|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Year2023\Day17; |
| 4 | + |
| 5 | +$start = microtime(true); |
| 6 | + |
| 7 | +$in = file_get_contents('example.txt', FILE_IGNORE_NEW_LINES); |
| 8 | +#$in = file_get_contents('input.txt', FILE_IGNORE_NEW_LINES); |
| 9 | + |
| 10 | +class AdventOfHeap extends \SplMinHeap |
| 11 | +{ |
| 12 | + protected function compare(mixed $value1, mixed $value2): int |
| 13 | + { |
| 14 | + return $value2['distance'] - $value1['distance']; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +class Solver { |
| 20 | + |
| 21 | + private array $map = []; |
| 22 | + private array $visited = []; |
| 23 | + private AdventOfHeap $heap; |
| 24 | + private string $finalCoord; |
| 25 | + |
| 26 | + private const DELTAS = [ |
| 27 | + 'N' => ['x' => 0, 'y' => -1], |
| 28 | + 'E' => ['x' => 1, 'y' => 0], |
| 29 | + 'S' => ['x' => 0, 'y' => 1], |
| 30 | + 'W' => ['x' => -1, 'y' => 0], |
| 31 | + ]; |
| 32 | + |
| 33 | + public function __construct(string $input) |
| 34 | + { |
| 35 | + $rows = explode("\n", $input); |
| 36 | + foreach ($rows as $row) { |
| 37 | + $this->map[] = str_split(trim($row)); |
| 38 | + } |
| 39 | + |
| 40 | + $this->finalCoord = count($this->map[0])-1 . "," . count($this->map)-1; |
| 41 | + |
| 42 | + $this->heap = new AdventOfHeap(); |
| 43 | + $this->heap->insert(['x' => 0, 'y' => 0, 'distance' => 0, 'direction' => 'E']); |
| 44 | + $this->heap->insert(['x' => 0, 'y' => 0, 'distance' => 0, 'direction' => 'S']); |
| 45 | + } |
| 46 | + |
| 47 | + private function getNeighbours(int $x, int $y, string $originalDirection): \Generator |
| 48 | + { |
| 49 | + switch ($originalDirection) { |
| 50 | + case "N": |
| 51 | + case "S": |
| 52 | + yield from $this->getNeighboursInDirection($x, $y, 'E'); |
| 53 | + yield from $this->getNeighboursInDirection($x, $y, 'W'); |
| 54 | + break; |
| 55 | + case "E": |
| 56 | + case "W": |
| 57 | + yield from $this->getNeighboursInDirection($x, $y, 'N'); |
| 58 | + yield from $this->getNeighboursInDirection($x, $y, 'S'); |
| 59 | + break; |
| 60 | + default: |
| 61 | + throw new \RuntimeException('invalid direction'); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private function getNeighboursInDirection(int $x, int $y, string $direction): \Generator |
| 66 | + { |
| 67 | + $distance = 0; |
| 68 | + for ($i = 1; $i <= 3; $i++) { |
| 69 | + $x += self::DELTAS[$direction]['x']; |
| 70 | + $y += self::DELTAS[$direction]['y']; |
| 71 | + if (isset($this->map[$y][$x])) { |
| 72 | + $distance += $this->map[$y][$x]; |
| 73 | + yield ['x' => $x, 'y' => $y, 'distance' => $distance, 'direction' => $direction]; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public function walk(): void |
| 79 | + { |
| 80 | + while (true) { |
| 81 | + $current = $this->heap->extract(); |
| 82 | + |
| 83 | + foreach ($this->getNeighbours($current['x'], $current['y'], $current['direction']) as $next) { |
| 84 | + $coord = $next['x'] . "," . $next['y']; |
| 85 | +# $coordDir = $next['x'] . "," . $next['y'] . "," . $next['direction']; |
| 86 | + if (isset($this->visited[$coord])) { // && $this->visited[$coord]['direction'] === $next['direction'] |
| 87 | + continue; |
| 88 | + } |
| 89 | + $next['distance'] += $current['distance']; |
| 90 | + $this->heap->insert($next); |
| 91 | + $this->visited[$coord] = ['distance' => $next['distance'], 'direction' => $next['direction']]; |
| 92 | + |
| 93 | + if ($coord === $this->finalCoord) { |
| 94 | + return; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public function getDistanceOfFinalTile(): int |
| 101 | + { |
| 102 | + return $this->visited[$this->finalCoord]['distance']; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +$solver = new Solver($in); |
| 107 | + |
| 108 | +$solver->walk(); |
| 109 | + |
| 110 | +echo $solver->getDistanceOfFinalTile(); |
| 111 | + |
| 112 | + |
| 113 | +echo "\n", (microtime(true) - $start), "\n"; |
0 commit comments