Advertisement
mosredna

AoC 2022 day 20

Dec 20th, 2022 (edited)
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs")
  2. const performance = require("perf_hooks").performance
  3. const eol = require("os").EOL
  4.  
  5. let startTime = performance.now()
  6. let part1 = (part2 = 0)
  7. const input = fs.readFileSync(__dirname + "/data.txt", "utf8").split(eol)
  8.  
  9. function decrypt(source, c = 1) {
  10.     const buf = [...source]
  11.     while (c--) {
  12.         for (const n of source) {
  13.             const index = buf.indexOf(n)
  14.             buf.splice(index, 1)
  15.             buf.splice((index + n.num) % buf.length, 0, n)
  16.         }
  17.     }
  18.     const zero = buf.findIndex(({ num }) => num === 0)
  19.     const numbers = [1000, 2000, 3000].map((k) => buf[(zero + k) % buf.length].num)
  20.     return numbers.reduce((s, a) => s + a, 0)
  21. }
  22.  
  23. let source = input.map((line) => ({ num: Number(line) }))
  24. part1 = decrypt(source)
  25. source = input.map((line) => ({ num: Number(line) * 811589153 }))
  26. part2 = decrypt(source, 10)
  27.  
  28. let time = performance.now() - startTime
  29. console.log(`Part 1: ${part1}\nPart 2: ${part2}\nTimer: ${time} ms`)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement