[2017-12-05] Solutions for both puzzles
parent
26b2da9f8f
commit
0adc00c3d1
@ -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…
Reference in New Issue