mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
2025 day 6 in PHP
This commit is contained in:
10
2025/day06/README.md
Normal file
10
2025/day06/README.md
Normal file
@@ -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
|
||||||
|
```
|
||||||
4
2025/day06/sample.txt
Normal file
4
2025/day06/sample.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
123 328 51 64
|
||||||
|
45 64 387 23
|
||||||
|
6 98 215 314
|
||||||
|
* + * +
|
||||||
81
2025/day06/solve.php
Executable file
81
2025/day06/solve.php
Executable file
@@ -0,0 +1,81 @@
|
|||||||
|
#!/usr/bin/env php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
if ($argc < 2) {
|
||||||
|
echo "Usage: {$argv[0]} INPUT\n";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$input = file($argv[1]);
|
||||||
|
|
||||||
|
function part1(string ...$input): int {
|
||||||
|
$lines = [];
|
||||||
|
|
||||||
|
foreach ($input as $line) {
|
||||||
|
$lines[] = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);
|
||||||
|
}
|
||||||
|
|
||||||
|
$lines = array_reverse($lines);
|
||||||
|
$cols = count($lines[0]);
|
||||||
|
$rows = count($lines);
|
||||||
|
|
||||||
|
$sum = 0;
|
||||||
|
|
||||||
|
for ($col = 0; $col < $cols; ++$col) {
|
||||||
|
if ($lines[0][$col] === '+') {
|
||||||
|
$acc = 0;
|
||||||
|
for ($row = 1; $row < $rows; ++$row) {
|
||||||
|
$acc += (int) $lines[$row][$col];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$acc = 1;
|
||||||
|
for ($row = 1; $row < $rows; ++$row) {
|
||||||
|
$acc *= (int) $lines[$row][$col];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$sum += $acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
function part2(string ...$lines): int {
|
||||||
|
$cols = strlen($lines[0]);
|
||||||
|
$rows = count($lines);
|
||||||
|
$numbers = array_fill(0, $cols, "");
|
||||||
|
|
||||||
|
for ($row = 0; $row < $rows - 1; ++$row) {
|
||||||
|
foreach (str_split($lines[$row]) as $col => $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";
|
||||||
Reference in New Issue
Block a user