You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//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
+
constMAX_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)
* 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
+
functionincreaseOctopusEnergy(&$group) {
50
+
foreach ($groupas$k_row => $row) {
51
+
foreach ($rowas$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
+
functionfindFlashes(&$group) {
71
+
foreach ($groupas$k_row => $row) {
72
+
foreach ($rowas$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)
//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
+
functiontriggerFlash(&$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 ($locationsaslist($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
0 commit comments