adventofcode/2020/9/XMASpt2.php

28 lines
503 B
PHP
Raw Permalink Normal View History

2022-12-01 21:02:00 +01:00
#!/usr/bin/env php
<?php
$lines = explode("\n", file_get_contents("input"));
$number = 248131121;
$sum = 0;
$start = 0;
for($i=0; $i<count($lines); $i++) {
$sum += intval($lines[$i]);
while($sum > $number)
$sum -= $lines[$start++];
if($sum === $number)
break;
}
$subset = array_slice($lines, $start, $i - $start);
sort($subset);
echo "Range starts on $start and ends on $i.\n";
echo "Lowest {$subset[0]} and highest " . array_pop($subset) . "\n";
echo "(you can sum them yourself)\n";
?>