[php] AOC day 11 part 2

Viewer

copydownloadembedprintName: AOC day 11 part 2
  1. #!/usr/local/bin/php
  2. <?php
  3.  
  4. class CellularSeats {
  5.     public $grid = [];
  6.     private $rows;
  7.     private $cols;
  8.  
  9.     public function __construct() {
  10.         $this->grid = file(__DIR__.'/input.txt');
  11.         $this->grid = array_map('trim', $this->grid);
  12.         $this->grid = array_map('str_split', $this->grid);
  13.         $this->rows = sizeof($this->grid);
  14.         $this->cols = sizeof($this->grid[0]);
  15.     }
  16.  
  17.     public function part2()
  18.     {
  19.         $grid = $this->grid;
  20.  
  21.         $changed = true;
  22.         $new_grid = $grid;
  23.         while($changed) {
  24.             $changed = false;
  25.             for ($line=0; $line < $this->rows; $line++) {
  26.                 for ($col=0; $col < $this->cols; $col++) {
  27.                     switch ($grid[$line][$col]) {
  28.                         // If a seat is empty (L) and there are no occupied seats adjacent to it, the seat becomes occupied.
  29.                         case 'L':
  30.                             if ($this->count_neighbors($grid, $line, $col) == 0) {
  31.                                 $new_grid[$line][$col] = '#';
  32.                                 $changed = true;
  33.                             }
  34.                             break;
  35.                         // If a seat is occupied (#) and four or more seats adjacent to it are also occupied, the seat becomes empty.
  36.                         // Also, people seem to be more tolerant than you expected: it now takes five or more visible occupied seats for an occupied seat to become empty
  37.                         case '#':
  38.                             if ($this->count_neighbors($grid, $line, $col) >= 5) {
  39.                                 $new_grid[$line][$col] = 'L';
  40.                                 $changed = true;
  41.                             }
  42.                             break;
  43.  
  44.                         // Otherwise, the seat's state does not change.
  45.                         default:
  46.                             break;
  47.                     }
  48.                 }
  49.             }
  50.             $grid = $new_grid;
  51.         }
  52.  
  53.         // count occupied seats
  54.         $grid = array_map('implode', $grid);
  55.         $grid = implode($grid);
  56.         return substr_count($grid, '#');
  57.     }
  58.  
  59.     private function count_neighbors($grid, $row, $col) {
  60.         $neighbors = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]];
  61.         $result = 0;
  62.         foreach ($neighbors as $offset) {
  63.             $factor = 1;
  64.             while (isset($grid[$row + $factor*$offset[0]][$col + $factor*$offset[1]]) && $grid[$row + $factor*$offset[0]][$col + $factor*$offset[1]] == '.') {
  65.                 $factor += 1;
  66.             }
  67.             if (isset($grid[$row + $factor*$offset[0]][$col + $factor*$offset[1]]) && $grid[$row + $factor*$offset[0]][$col + $factor*$offset[1]] == '#') {
  68.                 $result += 1;
  69.             }
  70.         }
  71.         return $result;
  72.     }
  73. }
  74.  
  75. $cs = new CellularSeats();
  76. $part2 = $cs->part2();
  77. echo("Part 2 occupied: $part2\n");

Editor

You can edit this paste and save as new:


File Description
  • AOC day 11 part 2
  • Paste Code
  • 11 Dec-2020
  • 2.93 Kb
You can Share it: