aoc2017/day5/main.php

59 lines
1.3 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;
}
}
}
echo 'First challange solution:'.PHP_EOL;
echo puzzle1(file_get_contents(dirname(__FILE__).'/input.txt')).PHP_EOL.PHP_EOL;
echo 'Second challange solution:'.PHP_EOL;
echo puzzle2(file_get_contents(dirname(__FILE__).'/input.txt')).PHP_EOL.PHP_EOL;