jamaik

Untitled

Dec 5th, 2021 (edited)
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //p=require("fs").readFileSync(`${__dirname}/inputs/day5.txt`,"utf8")
  2. //.replace(/\r\n/g,'\n').split("\n").map(d => d.split(/ -> |,/g).map(eval));
  3. //g={};s=0
  4. //f=(r,t,u,d)=>{[a,b]=r<t?[r,t]:[t,r];for(i=a;i<=b;i++){j=(d+"")[5]?u+"-"+i:i+"-"+(d*(i-r)+u);v=(g[j]|0)+1;g[j]=v;v==2&&s++}}
  5. //for([x,y,u,v]of p){k=(v-y)/(u-x);(k+"")[5]?f(y,v,x):f(x,u,y,k)}
  6. //console.log(s)
  7.  
  8. const points = require("fs")
  9.     .readFileSync(`${__dirname}/inputs/day5.txt`,"utf8")
  10.     .replace(/\r\n/g,'\n').split("\n")
  11.     .map(d => d.split(/ -> |,/g).map(eval));
  12.  
  13. const count = (skipDiagonal = false) => {
  14.     const grid = {};
  15.     let sum = 0;
  16.  
  17.     const loop = (start, end, constant, derivative) => {
  18.         const [a, b] = start < end ? [start, end] : [end, start];
  19.         for (let i = a; i <= b; i += 1) {
  20.             const [x, y] = derivative === undefined ? [constant, i] : [i, derivative * (i - start) + constant]
  21.             const j = `${x}-${y}`;
  22.             const nextVal = (grid[j] || 0) + 1;
  23.             grid[j] = nextVal;
  24.             if (nextVal === 2) {
  25.                 sum += 1;
  26.             }
  27.         }
  28.     }
  29.     for (const [x1, y1, x2, y2] of points) {
  30.         const d = (y2 - y1) / (x2 - x1);
  31.         if ((d+"")[5]) { // If k === Infinity || k === -Infinity
  32.             loop(y1, y2, x1)
  33.         } else {
  34.             if (skipDiagonal && [-1, 1].includes(d)) {
  35.                 continue;
  36.             }
  37.             loop(x1, x2, y1, d)
  38.         }
  39.     }
  40.     return sum;
  41. }
  42.  
  43. console.log(count(true), count())
Add Comment
Please, Sign In to add comment