Day 10 ported to PHP
This commit is contained in:
parent
9aa8450af7
commit
f3dec28731
1 changed files with 42 additions and 0 deletions
42
2020/10/main.php
Normal file
42
2020/10/main.php
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
<?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);
|
||||||
|
|
Loading…
Reference in a new issue