SHARE
TWEET

AoC2019/D3/JS

a guest Dec 3rd, 2019 308 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const _ = require('lodash')
  3. const raw = fs.readFileSync('inputs/day3.txt', 'utf8');
  4.  
  5. const [line1, line2] = raw.split('\n');
  6.  
  7. function setupSet(inputLine, outCoords, distances){
  8.     let x=0;
  9.     let y=0;
  10.     let distance = 1;  
  11.     for(inst of inputLine.split(',')){
  12.         const dir = inst.charAt(0);
  13.         const len = inst.substr(1)
  14.         if(dir=='R'){
  15.             for(let i=0; i<len; i++){
  16.                 x++;
  17.                 outCoords.push(`${x}_${y}`)
  18.                 distances[`${x}_${y}`] = distance;
  19.                 distance++;
  20.             }
  21.         }
  22.         if(dir=='L'){
  23.             for(let i=0; i<len; i++){
  24.                 x--;
  25.                 outCoords.push(`${x}_${y}`)
  26.                 distances[`${x}_${y}`] = distance;
  27.                 distance++;
  28.             }
  29.         }
  30.         if(dir=='U'){
  31.             for(let i=0; i<len; i++){
  32.                 y++;
  33.                 outCoords.push(`${x}_${y}`)
  34.                 distances[`${x}_${y}`] = distance;
  35.                 distance++;
  36.             }
  37.         }
  38.         if(dir=='D'){
  39.             for(let i=0; i<len; i++){
  40.                 y--;
  41.                 outCoords.push(`${x}_${y}`);
  42.                 distances[`${x}_${y}`] = distance;
  43.                 distance++;
  44.             }
  45.         }
  46.     }
  47. }
  48. const line1Coords = [];
  49. const line2Coords = [];
  50. const distances1 = {}
  51. const distances2 = {}
  52. setupSet(line1, line1Coords, distances1);
  53. setupSet(line2, line2Coords, distances2)
  54.  
  55.  
  56. const dists = []
  57. intersections = _.intersection(line1Coords, line2Coords);
  58.  
  59. for(point of intersections){
  60.     dists.push(_.sum(point.split('_').map(Number)))
  61. }
  62. console.log("Part 1:")
  63. console.log(_.min(dists))
  64.  
  65. let longest = 999999;
  66. for(elem of intersections){
  67.     let dist1 = distances1[elem];
  68.     let dist2 = distances2[elem];
  69.     if((dist1 + dist2) < longest)
  70.         longest = dist1 + dist2
  71. }
  72. console.log("Part 2:")
  73. console.log(longest)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top