Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Classes
  2. class Point {
  3.     constructor (x, y) {
  4.         this.x = x;
  5.         this.y = y;
  6.     }
  7.    
  8.     compare(other) {
  9.         let diff = this.x - other.x;
  10.        
  11.         if (diff == 0) {
  12.             diff = this.y - other.y;
  13.         }
  14.        
  15.         return diff;
  16.     }
  17. }
  18.  
  19. class Line {
  20.     constructor(start, end) {
  21.         this.start = start;
  22.         this.end = end;
  23.     }
  24.    
  25.     intersectionPoint (other) {
  26.    
  27.         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));
  28.        
  29.         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));
  30.        
  31.         if (ta >= 0 && ta <= 1 && tb >= 0 && tb <= 1) {
  32.             let x = this.start.x + ta * (this.end.x - this.start.x);
  33.             let y = this.start.y + ta * (this.end.y - this.start.y);
  34.             return new Point(x, y);
  35.         }
  36.         else {
  37.             return undefined;
  38.         }
  39.     }
  40.    
  41.     isPointOnLine (point) {
  42.         let cross = (point.y - this.start.y) * (this.end.x - this.start.x) - (point.x - this.start.x) * (this.end.y - this.start.y);
  43.        
  44.         return cross == 0;
  45.     }
  46.    
  47.     isPointOnLineSegment (point) {
  48.         let cross = (point.y - this.start.y) * (this.end.x - this.start.x) - (point.x - this.start.x) * (this.end.y - this.start.y);
  49.        
  50.         if (Math.abs(cross) != 0) {
  51.             return false;
  52.         }
  53.        
  54.         let sp = new Point(point.x - this.start.x, point.y - this.start.y);
  55.         let se = new Point(this.end.x - this.start.x, this.end.y - this.start.y);
  56.        
  57.         let dotAC = sp.x * se.x + sp.y * se.y;
  58.         let dotAB = se.x * se.x + se.y * se.y;
  59.        
  60.         return 0 < dotAC && dotAC < dotAB;
  61.     }
  62.    
  63.     manhattanDist () {
  64.         return Math.abs(this.end.x - this.start.x) + Math.abs(this.end.y - this.start.y);
  65.     }
  66. }
  67.  
  68. //program
  69.  
  70. function getDir (operation) {
  71.     switch (operation) {
  72.         case "U":
  73.             return new Point(0, 1);
  74.             break;
  75.            
  76.         case "D":
  77.             return new Point(0, -1);
  78.             break;
  79.            
  80.         case "L":
  81.             return new Point(-1, 0);
  82.             break;
  83.            
  84.         case "R":
  85.             return new Point(1, 0);
  86.             break;
  87.     }
  88. }
  89.  
  90. function generateLinesOnWire (wire) {
  91.     let lines = [];
  92.  
  93.     let cur = new Point(0,0);
  94.     let dir = new Point(0,0);
  95.     for (let i = 0; i < wire.length; i++) {
  96.         let operation = wire[i].substring(0,1);
  97.         let amount = parseInt(wire[i].substring(1,wire[i].length));
  98.         dir = getDir(operation);
  99.        
  100.         let start = new Point(cur.x, cur.y);
  101.         cur = new Point(cur.x + dir.x * amount, cur.y + dir.y * amount);
  102.         let end = new Point(cur.x, cur.y);
  103.        
  104.         lines.push(new Line(start, end));
  105.     }
  106.    
  107.     return lines;
  108. }
  109.  
  110. function CalculateIntersectionPointsOfWires(wire1, wire2) {
  111.     let points = [];
  112.    
  113.     for (let i = 0; i < wire2.length; i++) {
  114.         for (let j = 0; j < wire1.length; j++) {
  115.             let intersect = wire1[j].intersectionPoint(wire2[i]);
  116.             if (intersect != undefined) {
  117.                 points.push(intersect);
  118.             }
  119.         }
  120.     }
  121.    
  122.     return points;
  123. }
  124.  
  125. function run3_1 () {
  126.  
  127.  
  128.     let input = getInput(3,1);
  129.     let wires = input.split("\n");
  130.    
  131.     //Convert wires to points
  132.     let wire1Lines = generateLinesOnWire(wires[0].split(","));
  133.     let wire2Lines = generateLinesOnWire(wires[1].split(","));
  134.    
  135.    
  136.     //Calc points
  137.     let points = CalculateIntersectionPointsOfWires(wire1Lines, wire2Lines);
  138.    
  139.     //Find closest crosspoint
  140.     let closestDist = 99999999999;
  141.     for (let i = 0; i < points.length; i++) {
  142.         let point = points[i];
  143.    
  144.         let dist = Math.abs(point.x) + Math.abs(point.y);
  145.                
  146.         if (dist < closestDist && dist > 0) {
  147.             closestDist = dist;
  148.         }
  149.     }
  150.    
  151.     setOutput(3,1,"Dist: " + closestDist);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement