Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Classes
- class Point {
- constructor (x, y) {
- this.x = x;
- this.y = y;
- }
- compare(other) {
- let diff = this.x - other.x;
- if (diff == 0) {
- diff = this.y - other.y;
- }
- return diff;
- }
- }
- class Line {
- constructor(start, end) {
- this.start = start;
- this.end = end;
- }
- intersectionPoint (other) {
- let ta = ((other.start.y - other.end.y)*(this.start.x - other.start.x)+(other.end.x - other.start.x)*(this.start.y - other.start.y)) / ((other.end.x - other.start.x)*(this.start.y - this.end.y)-(this.start.x - this.end.x)*(other.end.y - other.start.y));
- let tb = ((this.start.y - this.end.y)*(this.start.x - other.start.x)+(this.end.x-this.start.x)*(this.start.y - other.start.y))/((other.end.x-other.start.x)*(this.start.y - this.end.y)-(this.start.x - this.end.x)*(other.end.y - other.start.y));
- if (ta >= 0 && ta <= 1 && tb >= 0 && tb <= 1) {
- let x = this.start.x + ta * (this.end.x - this.start.x);
- let y = this.start.y + ta * (this.end.y - this.start.y);
- return new Point(x, y);
- }
- else {
- return undefined;
- }
- }
- isPointOnLine (point) {
- let cross = (point.y - this.start.y) * (this.end.x - this.start.x) - (point.x - this.start.x) * (this.end.y - this.start.y);
- return cross == 0;
- }
- isPointOnLineSegment (point) {
- let cross = (point.y - this.start.y) * (this.end.x - this.start.x) - (point.x - this.start.x) * (this.end.y - this.start.y);
- if (Math.abs(cross) != 0) {
- return false;
- }
- let sp = new Point(point.x - this.start.x, point.y - this.start.y);
- let se = new Point(this.end.x - this.start.x, this.end.y - this.start.y);
- let dotAC = sp.x * se.x + sp.y * se.y;
- let dotAB = se.x * se.x + se.y * se.y;
- return 0 < dotAC && dotAC < dotAB;
- }
- manhattanDist () {
- return Math.abs(this.end.x - this.start.x) + Math.abs(this.end.y - this.start.y);
- }
- }
- //program
- function getDir (operation) {
- switch (operation) {
- case "U":
- return new Point(0, 1);
- break;
- case "D":
- return new Point(0, -1);
- break;
- case "L":
- return new Point(-1, 0);
- break;
- case "R":
- return new Point(1, 0);
- break;
- }
- }
- function generateLinesOnWire (wire) {
- let lines = [];
- let cur = new Point(0,0);
- let dir = new Point(0,0);
- for (let i = 0; i < wire.length; i++) {
- let operation = wire[i].substring(0,1);
- let amount = parseInt(wire[i].substring(1,wire[i].length));
- dir = getDir(operation);
- let start = new Point(cur.x, cur.y);
- cur = new Point(cur.x + dir.x * amount, cur.y + dir.y * amount);
- let end = new Point(cur.x, cur.y);
- lines.push(new Line(start, end));
- }
- return lines;
- }
- function CalculateIntersectionPointsOfWires(wire1, wire2) {
- let points = [];
- for (let i = 0; i < wire2.length; i++) {
- for (let j = 0; j < wire1.length; j++) {
- let intersect = wire1[j].intersectionPoint(wire2[i]);
- if (intersect != undefined) {
- points.push(intersect);
- }
- }
- }
- return points;
- }
- function run3_1 () {
- let input = getInput(3,1);
- let wires = input.split("\n");
- //Convert wires to points
- let wire1Lines = generateLinesOnWire(wires[0].split(","));
- let wire2Lines = generateLinesOnWire(wires[1].split(","));
- //Calc points
- let points = CalculateIntersectionPointsOfWires(wire1Lines, wire2Lines);
- //Find closest crosspoint
- let closestDist = 99999999999;
- for (let i = 0; i < points.length; i++) {
- let point = points[i];
- let dist = Math.abs(point.x) + Math.abs(point.y);
- if (dist < closestDist && dist > 0) {
- closestDist = dist;
- }
- }
- setOutput(3,1,"Dist: " + closestDist);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement