[2017-12-01] Solution for both puzzles
parent
944c86aa24
commit
c6fb65f9d2
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
// http://adventofcode.com/2017/day/1
|
||||
|
||||
function puzzle(string $input, bool $dynamicOffset) : int
|
||||
{
|
||||
$len = strlen($input);
|
||||
$offset = $dynamicOffset ? $len / 2 : 1;
|
||||
$output = 0;
|
||||
|
||||
// Duplicate input so we don't have to handle the circular event
|
||||
$input = $input.$input;
|
||||
|
||||
for ($x = 0; $x <= ($len - 1); $x++) {
|
||||
$currentNumber = (int) $input[$x];
|
||||
$nextNumber = (int) $input[$x + $offset];
|
||||
|
||||
// If we got the right number, add it to result
|
||||
if ($currentNumber === $nextNumber) {
|
||||
$output += $currentNumber;
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
Loading…
Reference in New Issue