Last active
January 5, 2016 16:09
-
-
Save ghaiklor/9457db7e96d985739006 to your computer and use it in GitHub Desktop.
Advent of Code (Day 3 Part 1)
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(''); | |
// Unique set of coordinates with the starting coordinates already added | |
const coordinates = new Set().add(`0x0`); | |
INPUT.reduce((curCoords, direction) => { | |
let newCoords = {x: 0, y: 0}; | |
if (direction === '^') newCoords = {x: curCoords.x, y: curCoords.y + 1}; | |
if (direction === 'v') newCoords = {x: curCoords.x, y: curCoords.y - 1}; | |
if (direction === '>') newCoords = {x: curCoords.x + 1, y: curCoords.y}; | |
if (direction === '<') newCoords = {x: curCoords.x - 1, y: curCoords.y}; | |
coordinates.add(`${newCoords.x}x${newCoords.y}`); | |
return newCoords; | |
}, {x: 0, y: 0}); | |
console.log(coordinates.size); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment