adventofcode/2020/10/main.php

42 lines
644 B
PHP

<?php
function diffs($jolts) {
sort($jolts);
array_unshift($jolts, 0);
$builtin = array_slice($jolts, -1)[0] + 3;
array_push($jolts, $builtin);
$c = array();
for ($i = 0; $i < count($jolts); $i++) {
$d = $jolts[$i+1] - $jolts[$i];
$c[$d]++;
}
return $c;
}
function totals($jolts) {
sort($jolts);
$c = array();
$c[0] = 1;
foreach ($jolts as $a) {
for ($i = 1; $i < 4; $i++)
$c[$a] += $c[$a-$i];
}
print_r($c);
}
$f = fopen("test.txt", "r");
$jolts = array();
while (!feof($f))
array_push($jolts, intval(fgets($f)));
fclose($f);
print_r($jolts);
$c = diffs($jolts);
print_r($c);
$c = totals($jolts);
print_r($c);