Advertisement
Guest User

Untitled

a guest
Dec 10th, 2021
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.49 KB | None | 0 0
  1.  
  2. $filename = "11.txt";
  3.  
  4. open(FILE, $filename);
  5. @data = <FILE>;
  6. close(FILE);
  7.  
  8. #Read in input as 2d array
  9.  
  10. for ($a = 0; $a < 10; $a++) {
  11.     $line = $data[$a];
  12.     $line =~ s/[^0-9]*//sgi;
  13.     @toprow = split("", $line);
  14.     for ($b = 0; $b < 10; $b++) {
  15.         $octo[$a][$b] = int($toprow[$b]);
  16.     }
  17. }
  18.  
  19.  
  20. $flashcount = 0;
  21.  
  22. for ($round = 0; $round < 1000000; $round++) { #Yeah 1 million is overkill, but the loop will surely exit when it found a step where all octopuses flashed.
  23.  
  24.     #Do +1 to all cells
  25.  
  26.     for ($b = 0; $b < 10; $b++) {
  27.     for ($a = 0; $a < 10; $a++) {
  28.         $octo[$a][$b]++;
  29.     }
  30.     }
  31.  
  32.     #While loop until no more changes to board can be made
  33.  
  34.     $changed = 1;
  35.     while ($changed == 1) {
  36.         $changed = 0;
  37.  
  38.         for ($b = 0; $b < 10; $b++) {
  39.         for ($a = 0; $a < 10; $a++) {
  40.  
  41.             if ($octo[$a][$b] > 9) { #Cell that need to flash
  42.                 $changed = 1;
  43.                 $octo[$a][$b] = -1; #Flash cell **AND** "lockout" cell so it cannot receive energy more.
  44.                 $flashcount++;
  45.  
  46.                 if ($a > 0) { #Boundary checks - if the cells are adjacent to the wall we shouldn't start flashing non-existent octopuses
  47.                     if ($b < 9) {
  48.                         if ($octo[$a - 1][$b + 1] > -1) { #Make sure cell isn't locked before receiving energy.
  49.                             $octo[$a - 1][$b + 1]++;
  50.                         }
  51.                     }
  52.                     if ($b > 0) {
  53.                         if ($octo[$a - 1][$b - 1] > -1) {
  54.                             $octo[$a - 1][$b - 1]++;
  55.                         }
  56.                     }
  57.                     if ($octo[$a - 1][$b] > -1) {
  58.                         $octo[$a - 1][$b]++;
  59.                     }
  60.                 }
  61.  
  62.                 if ($a < 9) {
  63.                     if ($b < 9) {
  64.                         if ($octo[$a + 1][$b + 1] > -1) {
  65.                             $octo[$a + 1][$b + 1]++;
  66.                         }
  67.                     }
  68.                     if ($b > 0) {
  69.                         if ($octo[$a + 1][$b - 1] > -1) {
  70.                             $octo[$a + 1][$b - 1]++;
  71.                         }
  72.                     }
  73.                     if ($octo[$a + 1][$b] > -1) {
  74.                         $octo[$a + 1][$b]++;
  75.                     }
  76.                 }
  77.  
  78.                 if ($b < 9) {
  79.                     if ($octo[$a][$b + 1] > -1) {
  80.                         $octo[$a][$b + 1]++;
  81.                     }
  82.                 }
  83.                 if ($b > 0) {
  84.                     if ($octo[$a][$b - 1] > -1) {
  85.                         $octo[$a][$b - 1]++;
  86.                     }
  87.                 }
  88.  
  89.             }
  90.  
  91.         }
  92.         }
  93.     }
  94.  
  95.     $indiflash = 0;
  96.     for ($b = 0; $b < 10; $b++) {
  97.     for ($a = 0; $a < 10; $a++) {
  98.         if ($octo[$b][$a] == -1) { #Check for locked cells
  99.             $octo[$b][$a] = 0; #Unlock cells
  100.             $indiflash++; #Count flashes during a step
  101.         }
  102.     }
  103.     }
  104.  
  105.     if ($indiflash == 100) { #All octopuses flashed at once
  106.         last; #Found answer to part2
  107.     }
  108.     if ($round == 99) {
  109.         $lastflashcount = $flashcount; #Found answer to part 1
  110.     }
  111.  
  112. }
  113.  
  114.  
  115. print $lastflashcount."\n";
  116. $round++; # $round starts from 0, while step in question starts from 1.
  117. print $round."\n";
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement