✨ Finit le 6e jours
This commit is contained in:
35
day_6/part_1.php
Normal file
35
day_6/part_1.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
include '../functions.php';
|
||||||
|
|
||||||
|
$array = array_map(fn($row) => array_filter(explode(' ', $row)), explode("\n", file_get_contents('./input.txt')));
|
||||||
|
|
||||||
|
$array = matrix_rotate_clockwise($array);
|
||||||
|
|
||||||
|
$sums = [];
|
||||||
|
foreach ($array as $row) {
|
||||||
|
$operation = array_shift($row);
|
||||||
|
if ($operation == '+') {
|
||||||
|
$data = array_sum($row);
|
||||||
|
}
|
||||||
|
if ($operation == '*') {
|
||||||
|
$data = array_reduce($row, function($carry, $item) {
|
||||||
|
$carry *= $item;
|
||||||
|
return $carry;
|
||||||
|
}, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sums[] = $data;
|
||||||
|
print_message(sprintf('%s = %s', implode($operation, $row), $data));
|
||||||
|
}
|
||||||
|
|
||||||
|
print_message(array_sum($sums));
|
||||||
|
|
||||||
|
function matrix_rotate_clockwise($matrix)
|
||||||
|
{
|
||||||
|
array_unshift($matrix, null);
|
||||||
|
$matrix = call_user_func_array('array_map', $matrix);
|
||||||
|
$matrix = array_map('array_reverse', $matrix);
|
||||||
|
|
||||||
|
return $matrix;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user