Advertisement
Guest User

Advent of code - Day 15 - Part one

a guest
Dec 15th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.29 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. $resp = floodFill(['x' => 0, 'y' => 0], $grid);
  30.  
  31. echo $resp . PHP_EOL;
  32.  
  33. function floodFill($startingPoint, $grid) {
  34.     $visited = array_fill(0, count($grid), array_fill(0, count($grid[0]), PHP_INT_MAX));
  35.     $queue = [$startingPoint];
  36.  
  37.     $visited[0][0] = 0;
  38.  
  39.     while (count($queue) > 0) {
  40.         $point = array_shift($queue);
  41.         $y = $point['y'];
  42.         $x = $point['x'];
  43.  
  44.         if ($y - 1 >= 0) {
  45.             if ($visited[$y -1][$x] > $visited[$y][$x] + $grid[$y-1][$x]) {
  46.                 $queue[] =
  47.                     [
  48.                         'y' => $y-1,
  49.                         'x' => $x
  50.                     ];
  51.  
  52.                 $visited[$y -1][$x] = $visited[$y][$x] + $grid[$y-1][$x];
  53.             }
  54.         }
  55.         // down
  56.         if ($y + 1 < count($grid)) {
  57.             if ($visited[$y + 1][$x] > $visited[$y][$x] + $grid[$y+1][$x]) {
  58.                 $queue[] = [
  59.                     'y' => $y+1,
  60.                     'x' => $x
  61.                 ];
  62.                 $visited[$y + 1][$x] = $visited[$y][$x] + $grid[$y+1][$x];
  63.             }
  64.         }
  65.         // left
  66.         if ($x - 1 >= 0) {
  67.  
  68.             if ($visited[$y][$x-1] > $visited[$y][$x] + $grid[$y][$x-1]) {
  69.                 $queue[] = [
  70.                     'y' => $y,
  71.                     'x' => $x-1
  72.                 ];
  73.                 $visited[$y][$x-1] = $visited[$y][$x] + $grid[$y][$x-1];
  74.             }
  75.         }
  76.         // right
  77.         if ($x + 1 < count($grid[0])) {
  78.             if ($visited[$y][$x+1] > $visited[$y][$x] + $grid[$y][$x+1]) {
  79.                 $queue[] = [
  80.                     'y' => $y,
  81.                     'x' => $x+1
  82.                 ];
  83.                 $visited[$y][$x+1] = $visited[$y][$x] + $grid[$y][$x+1];
  84.             }
  85.         }
  86.     }
  87.  
  88.     return $visited[count($grid)-1][count($grid[0])-1];
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement