Files
advent_of_code_2025/day_6/part_1.php
Clement Desmidt d9a6efe3c6 Finit le 6e jours
2026-01-12 15:21:15 +01:00

35 lines
846 B
PHP

<?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;
}