An urgent interrupt arrives from the CPU: it's trapped in a maze of jump instructions, and it would like assistance from any programs with spare cycles to help find the exit.
The message includes a list of the offsets for each jump. Jumps are relative: -1
moves to the previous instruction, and 2
skips the next one. Start at the first instruction in the list. The goal is to follow the jumps until one leads outside the list.
Read the full puzzle.
using System;
using System.Linq;
namespace AdventOfCode.Y2017.Day05;
[ProblemName("A Maze of Twisty Trampolines, All Alike")]
class Solution : Solver {
public object PartOne(string input) => GetStepCount(input, x => x + 1);
public object PartTwo(string input) => GetStepCount(input, x => x < 3 ? x + 1 : x - 1);
int GetStepCount(string input, Func<int, int> update) {
var numbers = input.Split('\n').Select(int.Parse).ToArray();
var i = 0;
var stepCount = 0;
while (i < numbers.Length && i >= 0) {
var jmp = numbers[i];
numbers[i] = update(numbers[i]);
i += jmp;
stepCount++;
}
return stepCount;
}
}
Please ☆ my repo if you like it!