[php] AOC day 11 part 1

Viewer

copydownloadembedprintName: AOC day 11 part 1
  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 part1()
  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.                         case '#':
  37.                             if ($this->count_neighbors($grid, $line, $col) >= 4) {
  38.                                 $new_grid[$line][$col] = 'L';
  39.                                 $changed = true;
  40.                             }
  41.                             break;
  42.  
  43.                         // Otherwise, the seat's state does not change.
  44.                         default:
  45.                             break;
  46.                     }
  47.                 }
  48.             }
  49.             $grid = $new_grid;
  50.         }
  51.  
  52.         // count occupied seats
  53.         $grid = array_map('implode', $grid);
  54.         $grid = implode($grid);
  55.         return substr_count($grid, '#');
  56.     }
  57.  
  58.     private function count_neighbors($grid, $row, $col) {
  59.         $neighbors = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]];
  60.         $result = 0;
  61.         foreach ($neighbors as $offset) {
  62.             if (isset($grid[$row + $offset[0]][$col + $offset[1]]) && $grid[$row + $offset[0]][$col + $offset[1]] == '#') {
  63.                 $result += 1;
  64.             }
  65.         }
  66.         return $result;
  67.     }
  68. }
  69.  
  70. $cs = new CellularSeats();
  71. $part1 = $cs->part1();
  72. echo("Part 1 occupied: $part1\n");

Editor

You can edit this paste and save as new:


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