Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require("fs")
- const performance = require("perf_hooks").performance
- const eol = require("os").EOL
- let startTime = performance.now()
- let part1 = (part2 = 0)
- const input = fs.readFileSync(__dirname + "/data.txt", "utf8").split(eol)
- function decrypt(source, c = 1) {
- const buf = [...source]
- while (c--) {
- for (const n of source) {
- const index = buf.indexOf(n)
- buf.splice(index, 1)
- buf.splice((index + n.num) % buf.length, 0, n)
- }
- }
- const zero = buf.findIndex(({ num }) => num === 0)
- const numbers = [1000, 2000, 3000].map((k) => buf[(zero + k) % buf.length].num)
- return numbers.reduce((s, a) => s + a, 0)
- }
- let source = input.map((line) => ({ num: Number(line) }))
- part1 = decrypt(source)
- source = input.map((line) => ({ num: Number(line) * 811589153 }))
- part2 = decrypt(source, 10)
- let time = performance.now() - startTime
- console.log(`Part 1: ${part1}\nPart 2: ${part2}\nTimer: ${time} ms`)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement