Skip to content

Instantly share code, notes, and snippets.

@mastermatt
Last active December 3, 2019 16:31
Show Gist options
  • Save mastermatt/a0fa08deea5ac2953f660be61d7d6a89 to your computer and use it in GitHub Desktop.
Save mastermatt/a0fa08deea5ac2953f660be61d7d6a89 to your computer and use it in GitHub Desktop.
AoC 2019
const fs = require("fs");
const path = require("path");
const [aWireDirs, bWireDirs] = fs
.readFileSync(path.resolve(__dirname, "./day3_input.txt"))
.toString()
.split("\n");
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
add(vector) {
return new Point(this.x + vector.x, this.y + vector.y);
}
}
const origin = new Point(0, 0);
class Line {
constructor(a, b) {
this.a = a;
this.b = b;
}
get isHorizontal() {
return this.a.y === this.b.y;
}
get length() {
return Line.length(this.a, this.b);
}
static length(a, b) {
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
}
intersection(other) {
if (this.isHorizontal === other.isHorizontal) {
return null;
}
const [h, v] = this.isHorizontal ? [this, other] : [other, this];
const between = (a, b, c) => (b < c ? a > b && a < c : a > c && a < b);
if (between(v.a.x, h.a.x, h.b.x) && between(h.a.y, v.a.y, v.b.y)) {
return new Point(v.a.x, h.a.y);
}
return null;
}
}
const dirToVector = input => {
const [dir, ...rest] = input;
let dis = Number(rest.join(""));
if (dir === "D" || dir === "L") {
dis *= -1;
}
return dir === "R" || dir === "L" ? new Point(dis, 0) : new Point(0, dis);
};
const dirsToWire = dirs => {
let start = origin;
return dirs.map(dir => {
const end = start.add(dirToVector(dir));
const line = new Line(start, end);
start = end;
return line;
});
};
const aWire = dirsToWire(aWireDirs.split(","));
const bWire = dirsToWire(bWireDirs.split(","));
const distancesToOrigin = [];
const combinedStepsToIntersections = [];
let wireBDist = 0;
for (const bLine of bWire) {
let wireADist = 0;
for (const aLine of aWire) {
const intersection = aLine.intersection(bLine);
if (intersection) {
distancesToOrigin.push(Line.length(origin, intersection));
combinedStepsToIntersections.push(
wireADist +
wireBDist +
Line.length(aLine.a, intersection) +
Line.length(bLine.a, intersection)
);
}
wireADist += aLine.length;
}
wireBDist += bLine.length;
}
console.log("##### part one", Math.min(...distancesToOrigin));
console.log("##### part two", Math.min(...combinedStepsToIntersections));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment