Advertisement
Guest User

Advent of code - Day 15 - Part two

a guest
Dec 15th, 2021
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 KB | None | 0 0
  1. <?php
  2.  
  3. function readInput(): array
  4. {
  5.     $lines = [];
  6.  
  7.     $f = fopen( 'php://stdin', 'r' );
  8.  
  9.     while( $line = fgets( $f ) ) {
  10.         $withOutNewLine = str_replace(PHP_EOL,'',$line);
  11.         if ($withOutNewLine != '') {
  12.             $lines[] = $withOutNewLine;
  13.         }
  14.     }
  15.  
  16.     fclose($f);
  17.  
  18.     return $lines;
  19. }
  20.  
  21. $input = readInput();
  22.  
  23. $grid = [];
  24.  
  25. foreach ($input as $line) {
  26.     $grid[] = array_map(fn($item) => intval($item), str_split($line));
  27. }
  28.  
  29. $grid = expandGrid($grid);
  30.  
  31. $resp = floodFill(['x' => 0, 'y' => 0], $grid);
  32.  
  33. echo $resp . PHP_EOL;
  34.  
  35. function expandGrid($grid)
  36. {
  37.     $w = count($grid[0]);
  38.     $l = count($grid);
  39.  
  40.     $newW = $w*5;
  41.     $newL = $l*5;
  42.  
  43.     $newGrid = array_fill(0, $l * 5, array_fill(0, $w * 5, 0));
  44.  
  45.  
  46.     for ($i=0; $i < $l; $i++) {
  47.         for ($j=0; $j < $w; $j++) {
  48.             $newGrid[$i][$j] = $grid[$i][$j];
  49.         }
  50.     }
  51.  
  52.     for ($i=0; $i < $newL; $i++) {
  53.         for ($j=$w; $j < $newW; $j++) {
  54.             $newGrid[$i][$j] = $newGrid[$i][$j-$w] + 1;
  55.  
  56.             if ($newGrid[$i][$j] > 9) {
  57.                 $newGrid[$i][$j] = 1;
  58.             }
  59.         }
  60.     }
  61.  
  62.     for ($i=$l; $i < $newL; $i++) {
  63.         for ($j=0; $j < $newW; $j++) {
  64.             $newGrid[$i][$j] = $newGrid[$i-$l][$j] + 1;
  65.  
  66.             if ($newGrid[$i][$j] > 9) {
  67.                 $newGrid[$i][$j] = 1;
  68.             }
  69.         }
  70.     }
  71.  
  72.     return $newGrid;
  73. }
  74.  
  75. function floodFill($startingPoint, $grid) {
  76.     $visited = array_fill(0, count($grid), array_fill(0, count($grid[0]), PHP_INT_MAX));
  77.     $queue = [$startingPoint];
  78.  
  79.     $visited[0][0] = 0;
  80.  
  81.     while (count($queue) > 0) {
  82.         $point = array_shift($queue);
  83.         $y = $point['y'];
  84.         $x = $point['x'];
  85.  
  86.         if ($y - 1 >= 0) {
  87.             if ($visited[$y -1][$x] > $visited[$y][$x] + $grid[$y-1][$x]) {
  88.                 $queue[] =
  89.                     [
  90.                         'y' => $y-1,
  91.                         'x' => $x
  92.                     ];
  93.  
  94.                 $visited[$y -1][$x] = $visited[$y][$x] + $grid[$y-1][$x];
  95.             }
  96.         }
  97.         // down
  98.         if ($y + 1 < count($grid)) {
  99.             if ($visited[$y + 1][$x] > $visited[$y][$x] + $grid[$y+1][$x]) {
  100.                 $queue[] = [
  101.                     'y' => $y+1,
  102.                     'x' => $x
  103.                 ];
  104.                 $visited[$y + 1][$x] = $visited[$y][$x] + $grid[$y+1][$x];
  105.             }
  106.         }
  107.         // left
  108.         if ($x - 1 >= 0) {
  109.  
  110.             if ($visited[$y][$x-1] > $visited[$y][$x] + $grid[$y][$x-1]) {
  111.                 $queue[] = [
  112.                     'y' => $y,
  113.                     'x' => $x-1
  114.                 ];
  115.                 $visited[$y][$x-1] = $visited[$y][$x] + $grid[$y][$x-1];
  116.             }
  117.         }
  118.         // right
  119.         if ($x + 1 < count($grid[0])) {
  120.             if ($visited[$y][$x+1] > $visited[$y][$x] + $grid[$y][$x+1]) {
  121.                 $queue[] = [
  122.                     'y' => $y,
  123.                     'x' => $x+1
  124.                 ];
  125.                 $visited[$y][$x+1] = $visited[$y][$x] + $grid[$y][$x+1];
  126.             }
  127.         }
  128.     }
  129.  
  130.     return $visited[count($grid)-1][count($grid[0])-1];
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement