Last active
December 3, 2019 16:31
-
-
Save mastermatt/a0fa08deea5ac2953f660be61d7d6a89 to your computer and use it in GitHub Desktop.
Revisions
-
mastermatt revised this gist
Dec 3, 2019 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -73,15 +73,15 @@ const dirsToWire = dirs => { }); }; 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)); @@ -98,5 +98,5 @@ for (const bLine of bWires) { wireBDist += bLine.length; } console.log("##### part one", Math.min(...distancesToOrigin)); console.log("##### part two", Math.min(...combinedStepsToIntersections)); -
mastermatt created this gist
Dec 3, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,102 @@ 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 aWires = dirsToWire(aWireDirs.split(",")); const bWires = dirsToWire(bWireDirs.split(",")); const distancesToOrigin = []; const combinedStepsToIntersections = []; let wireBDist = 0; for (const bLine of bWires) { let wireADist = 0; for (const aLine of aWires) { 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)); // 1195 console.log("##### part two", Math.min(...combinedStepsToIntersections)); // 91518