Advertisement
Guest User

2023/24 - no Z3

a guest
Dec 24th, 2023
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const utils = require('../../utils')
  2. const data = utils.byLine('./input.txt').map(intsS)
  3. const USE = 10
  4. const MIN = 200000000000000
  5. const MAX = 400000000000000
  6. const SUB = data.slice(0, USE)
  7.  
  8. function findLineLambda(a, b, c, d, p, q, r, s) {
  9.     const det = (c - a) * (s - q) - (r - p) * (d - b);
  10.     if (det === 0) return null  
  11.     return Math.round(((s - q) * (r - a) + (p - r) * (s - b)) / det)
  12. }
  13.  
  14. function findIntersectionPoint(a, b, d1 = 0, d2 = 1) {
  15.     let lambda = findLineLambda(
  16.         a[d1], a[d2], a[d1] + a[d1 + 3], a[d2] + a[d2 + 3],
  17.         b[d1], b[d2], b[d1] + b[d1 + 3], b[d2] + b[d2 + 3],
  18.     )
  19.     if (lambda === null) return null
  20.  
  21.     let f = a[d1] + lambda * a[d1 + 3]
  22.     let g = a[d2] + lambda * a[d2 + 3]
  23.     return [f, g]
  24. }
  25.  
  26. function findCommonIntersection(v, d1 = 0, d2 = 1) {
  27.     let viable = true
  28.     let current
  29.     const transformed = SUB.map(line => {
  30.         const copy = [...line]
  31.         copy[d1 + 3] += v[0]
  32.         copy[d2 + 3] += v[1]
  33.         return copy
  34.     })
  35.     utils.pairs(transformed, (a, b) => {
  36.         if (!viable) return false
  37.         const point = findIntersectionPoint(a, b, d1, d2)
  38.         if (point === null) return
  39.         if (!current) current = point
  40.  
  41.         viable = point[0] === current[0] && point[1] === current[1]
  42.     })
  43.     if (!viable) return false
  44.     return current
  45. }
  46.  
  47. let intersections = 0
  48. utils.pairs(data, (a, b) => {
  49.     let p = findIntersectionPoint(a, b)
  50.     if (!p) return
  51.  
  52.     let [x, y] = findIntersectionPoint(a, b, 0, 1)
  53.     if (x < MIN || x > MAX || y < MIN || y > MAX) return // out of bounds
  54.     if ((x < a[0] && a[3] > 0) || (x > a[0] && a[3] < 0)) return // in the past for A
  55.     if ((x < b[0] && b[3] > 0) || (x > b[0] && b[3] < 0)) return // in the past for B
  56.     intersections++
  57. })
  58.  
  59. function solve() {
  60.     for (let x = 0; x <= Infinity; x++) {
  61.         for (let y = 0; y <= x; y++) {
  62.             for (let [sx, sy] of [[1, 1], [1, -1], [-1, 1], [-1, -1]]) {
  63.                 // X and Y have an intersection
  64.                 let xy = findCommonIntersection([x * sx, y * sy], 0, 1)
  65.                 if (!xy) continue
  66.  
  67.                 for (let z = 0; z <= Infinity; z++) {
  68.                     for (let sz of [1, -1]) {
  69.                         // find intersection for X and Z, if it exists, it's the result
  70.                         let xz = findCommonIntersection([x * sx, z * sz], 0, 2)
  71.                         if (!xz) continue
  72.                         return xy[0] + xy[1] + xz[1]
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.     }
  78. }
  79.  
  80. console.log('Part 1', intersections)
  81. console.log('Part 2', solve())
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement