aoc2017/day5/main.php

53 lines
1.1 KiB
PHP

<?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;
}
}
}