re: AoC Day 1: Chronal Calibration VIEW POST

FULL DISCUSSION
 

PHP

Part 1:

<?php
$freq = 0;
$list = file_get_contents($argv[1]);
$changes = explode("\n", trim($list));
foreach ($changes as $change) {
    $freq += intval($change);
}
echo $freq;
die(1);

Part 2:

<?php
$freq = 0;
$seen = array(0);
$current = 0;
$newfreq = null;
$list = file_get_contents($argv[1]);
$changes = explode("\n", trim($list));
do {
    if ($newfreq) {
        array_push($seen, $newfreq);
    }
    $freq += intval($changes[$current]);
    $newfreq = $freq;
    $current++;
    if ($current == count($changes)) {
        $current = 0;
    }
} while (!in_array($newfreq, $seen));
echo $freq;
die(1);
code of conduct - report abuse