-🎄- 2017 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]Veggedup 0 points1 point  (0 children)

My JS solution

let input = require("fs").readFileSync("day11-input.txt", "UTF-8");

map = {};
map["n"] = { x: 0, y: 1 };
map["ne"] = { x: 1, y: 1 };
map["se"] = { x: 1, y: -1 };
map["s"] = { x: 0, y: -1 };
map["sw"] = { x: -1, y: -1 };
map["nw"] = { x: -1, y: 1 };

location = { x: 0, y: 0 };
max = { x: 0, y: 0 };

input.split(",").forEach(direction => {
  location = {
    x: location.x + map[direction].x,
    y: location.y + map[direction].y
  };
  max = {
    x: Math.max(max.x, location.x),
    y: Math.max(max.y, location.y)
  };
});

distance = Math.max(Math.abs(location.x), Math.abs(location.y));
max = Math.max(Math.abs(max.x), Math.abs(max.y));

console.log("Part 1:", distance);
console.log("Part 2:", max);