Last active
January 5, 2016 16:11
-
-
Save ghaiklor/1a0b24f8c81f34939981 to your computer and use it in GitHub Desktop.
Advent of Code (Day 3 Part 2)
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
const fs = require('fs'); | |
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split(''); | |
const santaDirections = INPUT.filter((item, index) => index % 2 === 0); | |
const roboSantaDirections = INPUT.filter((item, index) => index % 2 === 1); | |
// Get array of directions and return array of visited coordinates | |
const traverse = directions => { | |
let visitedCoordinates = ['0x0']; | |
let currentPosition = {x: 0, y: 0}; | |
directions.forEach(direction => { | |
if (direction === '^') currentPosition.y++; | |
if (direction === 'v') currentPosition.y--; | |
if (direction === '>') currentPosition.x++; | |
if (direction === '<') currentPosition.x--; | |
visitedCoordinates.push(`${currentPosition.x}x${currentPosition.y}`); | |
}); | |
return visitedCoordinates; | |
}; | |
const result = new Set(traverse(santaDirections).concat(traverse(roboSantaDirections))).size; | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment