Skip to content

Instantly share code, notes, and snippets.

@ghaiklor
Last active January 5, 2016 16:11
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 ghaiklor/1a0b24f8c81f34939981 to your computer and use it in GitHub Desktop.
Save ghaiklor/1a0b24f8c81f34939981 to your computer and use it in GitHub Desktop.
Advent of Code (Day 3 Part 2)
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