Skip to content

Commit 709d387

Browse files
author
Adrian Reimann
committed
refactored some while loops to neater for loops
1 parent 870861d commit 709d387

File tree

3 files changed

+5
-10
lines changed

3 files changed

+5
-10
lines changed

day_04/4-1.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ function markBoard(&$board, $number) {
5353
}
5454

5555
//after we've replaced all numbers of the board, check the columns (we already checked rows when going per row)
56-
$i = 0;
57-
while($i < 5) { //loop through columns up to 5 (cannot use count($row) as some rows may have gotten shorter)
58-
$column = array_column($board, $i++);
56+
for($i = 0; $i < 5; $i++) { //loop through columns up to 5 (cannot use count($row) as some rows may have gotten shorter)
57+
$column = array_column($board, $i);
5958
if(array_sum($column) == 0) {
6059
return true; //winner by column!
6160
}

day_04/4-2.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ function markBoard(&$board, $number) {
6262
}
6363

6464
//after we've replaced all numbers of the board, check the columns (we already checked rows when going per row)
65-
$i = 0;
66-
while($i < 5) { //loop through columns up to 5 (cannot use count($row) as some rows may have gotten shorter)
67-
$column = array_column($board, $i++);
65+
for($i = 0; $i < 5; $i++) { //loop through columns up to 5 (cannot use count($row) as some rows may have gotten shorter)
66+
$column = array_column($board, $i);
6867
if(array_sum($column) == 0) {
6968
return true; //winner by column!
7069
}

day_13/13-2.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,14 @@ function fillEmptyFields($arr, $filler = '.') {
171171
}
172172

173173
//fill every row of the array with the "empty" filler
174-
$i = 0;
175174
$full_arr = [];
176-
while($i <= $highest_y) { //<= as we start from 0
175+
for($i = 0; $i <= $highest_y ; $i++) { //<= as we start from 0
177176
$full_arr[$i] = array_fill(0 , $highest_x+1, $filler); //dot will be our "empty" fields (highest_x+1 as we start from 0)
178177

179178
//if this row exists on the array of actual values, here is the time where we replace the "empty" fields of that row with actual fields
180179
if(isset($arr[$i])) {
181180
$full_arr[$i] = array_replace($full_arr[$i], $arr[$i]);
182181
}
183-
184-
$i++;
185182
}
186183

187184
//return the filled up array

0 commit comments

Comments
 (0)