Skip to content

Instantly share code, notes, and snippets.

@incon
Created December 5, 2017 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save incon/9e19e8948aa3c461d2be1573d7feea4b to your computer and use it in GitHub Desktop.
Save incon/9e19e8948aa3c461d2be1573d7feea4b to your computer and use it in GitHub Desktop.
function part1(str) {
let numbers = str.split("\n").map(num => parseInt(num));
let count = 0;
let nextIndex = 0;
let mazeSize = numbers.length;
while (nextIndex < mazeSize) {
count++;
const currentIndex = nextIndex;
nextIndex = numbers[currentIndex] + currentIndex;
numbers[currentIndex] = numbers[currentIndex] + 1;
}
return count;
}
function part2(str) {
let numbers = str.split("\n").map(num => parseInt(num));
let count = 0;
let nextIndex = 0;
let mazeSize = numbers.length;
while (nextIndex < mazeSize) {
count++;
const currentIndex = nextIndex;
nextIndex = numbers[currentIndex] + currentIndex;
numbers[currentIndex] =
numbers[currentIndex] + (numbers[currentIndex] >= 3 ? -1 : 1);
}
return count;
}
const input = require("fs").readFileSync("day5-input.txt", "UTF-8");
console.log("Part1:", part1(input));
console.log("Part2:", part2(input));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment