Skip to content

Commit 673b340

Browse files
author
Adrian Reimann
committed
solution for day 11 part 2
1 parent 3f95147 commit 673b340

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

day_11/11-1.php

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const MAX_ENERGY = 9; //if charge is higher than this, the octopus flashes
44
const MAX_STEPS = 100; //these are the steps we wanna run
55

6-
76
//read input and format it into multidimensional of octopuses (10 by 10)
87
$octopus_group = [];
98
$input = fopen("input.txt", "r");

day_11/11-2.php

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
//SAME CODE AS PART 1 solution - just instead of looping a fixed x times, we just loop until the flash count of a single loop is exactly 100 (the count of octopuses)
4+
5+
const MAX_ENERGY = 9; //if charge is higher than this, the octopus flashes
6+
7+
//read input and format it into multidimensional of octopuses (10 by 10)
8+
$octopus_group = [];
9+
$octopus_count = 0; //with the given 10x10 grids this will always be 100 - but checking it during input makes the code dynamic for different size grids
10+
$input = fopen("input.txt", "r");
11+
while (($input_line = fgets($input)) !== false) {
12+
$split_line = str_split(trim($input_line)); //split numbers into array
13+
$octopus_group[] = $split_line; //add array to octopus_group array
14+
$octopus_count += count($split_line);
15+
}
16+
fclose($input);
17+
18+
$steps = 0;
19+
$flash_count = 0;
20+
//loop until the flash count of the last loop was exactly same as octopus count
21+
while ($flash_count != $octopus_count) {
22+
$steps++; //increase steps
23+
$flash_list = [];
24+
25+
//first, increase all octopuse values by 1
26+
increaseOctopusEnergy($octopus_group);
27+
28+
//now recursively check for flashes
29+
findFlashes($octopus_group);
30+
31+
//lastly, set the flashed octopuses to zero (this is a good time to count how many octopuses flashed, so we pass the flash list to see WHICH flashed)
32+
$flash_count = resetFlashedOctopuses($octopus_group, $flash_list);
33+
}
34+
35+
//result
36+
echo "At " . $steps . " steps, all octopuses synchronise." . PHP_EOL;
37+
38+
39+
/**
40+
* Loop through the multidimensional array and increase the energy level of every octopus by 1
41+
*
42+
* @author Adrian
43+
* @date_created 2021-12-12
44+
*
45+
* @param array $group the multidimensional array of octopuses
46+
*
47+
* @return array
48+
*/
49+
function increaseOctopusEnergy(&$group) {
50+
foreach ($group as $k_row => $row) {
51+
foreach ($row as $k_octo => $octopus) {
52+
$group[$k_row][$k_octo]++;
53+
}
54+
}
55+
56+
return $group;
57+
}
58+
59+
/**
60+
* Loop through the multidimensional array and trigger a flash if energy level reached MAX.
61+
* This calls a recursive function that checks followup-flashes from surrounding octopuses.
62+
*
63+
* @author Adrian
64+
* @date_created 2021-12-12
65+
*
66+
* @param array $group the multidimensional array of octopuses
67+
*
68+
* @return array
69+
*/
70+
function findFlashes(&$group) {
71+
foreach ($group as $k_row => $row) {
72+
foreach ($row as $k_octo => $octopus) {
73+
//access the octopus from array instead of $octopus, as the referenced array may have been modified in the recursive function - this does not reflect on $row in this loop yet
74+
if($group[$k_row][$k_octo] > MAX_ENERGY) {
75+
triggerFlash($group, $k_octo, $k_row);
76+
}
77+
}
78+
}
79+
80+
return $group;
81+
}
82+
83+
/**
84+
* Loop through the multidimensional array and reset every "null" value back to 0 so it is a natural number again
85+
*
86+
* @author Adrian
87+
* @date_created 2021-12-12
88+
*
89+
* @param array $group the multidimensional array of octopuses
90+
*
91+
* @return int how many octopuses where reset (read: how many octopuses flashed)
92+
*/
93+
function resetFlashedOctopuses(&$group, &$flash_list) {
94+
$reset_count = 0;
95+
foreach ($group as $k_row => $row) {
96+
foreach ($row as $k_octo => $octopus) {
97+
if($octopus == null) {
98+
//reset the energy to 0 and increase the reset (flash) counter
99+
$group[$k_row][$k_octo] = 0;
100+
$reset_count++;
101+
}
102+
}
103+
}
104+
105+
return $reset_count;
106+
}
107+
108+
/**
109+
* Loop through the multidimensional array and trigger a flash if energy level reached MAX.
110+
* This calls a recursive function that checks followup-flashes from surrounding octopuses.
111+
*
112+
* @author Adrian
113+
* @date_created 2021-12-12
114+
*
115+
* @param array $group the multidimensional array of octopuses
116+
* @param int $x_loc array location of this octopus on the "inner" (second) array
117+
* @param int $y_loc array location of this octopus on the "outer" (first) array
118+
*
119+
*/
120+
function triggerFlash(&$group, $x_loc, $y_loc) {
121+
122+
//set this octopus to "null" instead of a number, as an octopus flash cannot be triggered twice in the same step, so we do NOT want to increase more after a flash
123+
$group[$y_loc][$x_loc] = null;
124+
125+
//array of all locations the flash would affect/increase (array of array as some $x keys are the same and would overwrite)
126+
$locations = [
127+
[$x_loc-1, $y_loc-1], //top left
128+
[$x_loc, $y_loc-1], //top middle
129+
[$x_loc+1, $y_loc-1], //top right
130+
[$x_loc-1, $y_loc], //left
131+
[$x_loc+1, $y_loc], //right
132+
[$x_loc-1, $y_loc+1], //bottom left
133+
[$x_loc, $y_loc+1], //bottom
134+
[$x_loc+1, $y_loc+1], //bottom right
135+
];
136+
137+
//only increase surrounding values if isset - this not only skips outer bounds, but also the octopuses that already flashed this step, as they are now "null"
138+
foreach ($locations as list($x, $y)) {
139+
if(isset($group[$y][$x])) {
140+
$group[$y][$x]++;
141+
//if the increase surpasses max energy, trigger a new flash from this octopus
142+
if($group[$y][$x] > MAX_ENERGY) {
143+
triggerFlash($group, $x, $y);
144+
}
145+
}
146+
}
147+
}

0 commit comments

Comments
 (0)