Skip to content

Instantly share code, notes, and snippets.

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