[2017-12-01] Solution for both puzzles

master
Elis Hirwing 5 years ago
parent 944c86aa24
commit c6fb65f9d2
Signed by: etu
GPG Key ID: D57EFA625C9A925F
  1. 2
      Makefile
  2. 24
      day1/main.php

@ -1,6 +1,8 @@
.PHONY: day1 day2 day3 day4 day5 day6 day7 day8 day9 day10 day11 day12 day13 day14 day15 day16 day17 day18 day19 day20 day21 day22 day23 day24 day25
day1:
php day1/main.php
day2:
day3:
day4:

@ -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…
Cancel
Save