You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aoc2017/day5/main.php

53 lines
1.1 KiB

<?php
// https://adventofcode.com/2017/day/5
function puzzle1(string $input)
{
$jumps = 0;
$placement = 0;
$instructions = array_filter(explode(PHP_EOL, $input));
while (true) {
// Get current jump size
$jumpSize = $instructions[$placement];
// Increment jump size
$instructions[$placement]++;
// Do jump
$placement += $jumpSize;
$jumps++;
if ($placement >= count($instructions)) {
return $jumps;
}
}
}
function puzzle2(string $input)
{
$jumps = 0;
$placement = 0;
$instructions = array_filter(explode(PHP_EOL, $input));
while (true) {
// Get current jump size
$jumpSize = $instructions[$placement];
// Update instructions
if ($instructions[$placement] >= 3) {
$instructions[$placement]--;
} else {
$instructions[$placement]++;
}
// Do jump
$placement += $jumpSize;
$jumps++;
if ($placement >= count($instructions)) {
return $jumps;
}
}
}