diff --git a/day_6/part_1.php b/day_6/part_1.php new file mode 100644 index 0000000..d3fc564 --- /dev/null +++ b/day_6/part_1.php @@ -0,0 +1,35 @@ + 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; +} \ No newline at end of file