Created
December 5, 2017 06:26
-
-
Save incon/9e19e8948aa3c461d2be1573d7feea4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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