diff --git a/2025/day06/README.md b/2025/day06/README.md new file mode 100644 index 0000000..66f42fe --- /dev/null +++ b/2025/day06/README.md @@ -0,0 +1,10 @@ +# Day 06: PHP + +Simple and straight-forward. This is a normal programming language. + + +```console +$ ./solve.php sample.txt +Part1: 4277556 +Part2: 3263827 +``` diff --git a/2025/day06/sample.txt b/2025/day06/sample.txt new file mode 100644 index 0000000..337b837 --- /dev/null +++ b/2025/day06/sample.txt @@ -0,0 +1,4 @@ +123 328 51 64 + 45 64 387 23 + 6 98 215 314 +* + * + diff --git a/2025/day06/solve.php b/2025/day06/solve.php new file mode 100755 index 0000000..2c0d6ae --- /dev/null +++ b/2025/day06/solve.php @@ -0,0 +1,81 @@ +#!/usr/bin/env php + $c) { + if ($c !== ' ') { + $numbers[$col] .= $c; + } + } + } + + $sum = 0; + + foreach (str_split($lines[$rows - 1]) as $col => $c) { + switch ($c) { + case '+': + $acc = 0; + for ($i = $col; $i < $cols && $numbers[$i] !== ""; ++$i) { + $acc += (int) $numbers[$i]; + } + $sum += $acc; + break; + case '*': + $acc = 1; + for ($i = $col; $i < $cols && $numbers[$i] !== ""; ++$i) { + $acc *= (int) $numbers[$i]; + } + $sum += $acc; + break; + } + } + + return $sum; +} + +echo "Part1: " . part1(...$input) . "\n"; +echo "Part2: " . part2(...$input) . "\n";