[2017-12-05] Solutions for both puzzles

master
Elis Hirwing 6 years ago
parent 26b2da9f8f
commit 0adc00c3d1
Signed by: etu
GPG Key ID: D57EFA625C9A925F

@ -13,6 +13,8 @@ day4:
php day4/main.php
day5:
php day5/main.php
day6:
day7:
day8:

@ -0,0 +1,52 @@
<?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;
}
}
}
Loading…
Cancel
Save