Advent of Code 2022 - Day 20: Grove Positioning System Solution
Handling indexes with carePart 1
Trick here is to wrap each element into an array so that we could make use of reference. Hence, we could freely swap elements but still be able to keep track of original ones
For moving elements, case of positive value, new place's index is original one plus the value itself. To handle overflow, simply modulo the length of the array
Case of negative value is the same, we only have to reverse the array, do the move like the case of positive value, and reverse back
Implementation
1const fs = require("fs")23const readData = () => {4 const data = fs.readFileSync("./input", "utf-8").split(/\r?\n/).map(Number)56 return data7}89const main = () => {10 const data = readData()1112 const mix = arr => {13 const arrLength = arr.length14 const refs = arr.map(val => [val])15 const original = [...refs]1617 for (const currentRef of original) {18 const value = currentRef[0]19 const isNegative = value < 020 if (isNegative) refs.reverse()21 const currentIndex = refs.findIndex(ref => ref === currentRef)22 const newIndex = (currentIndex + Math.abs(value)) % (arrLength - 1)23 refs.splice(currentIndex, 1)24 refs.splice(newIndex, 0, currentRef)25 if (isNegative) refs.reverse()26 }2728 return refs.flat()29 }3031 const findValueAt = (index, arr) => {32 const arrLength = arr.length33 const indexOfZero = arr.findIndex(val => val === 0)34 index = (indexOfZero + index) % arrLength35 return arr[index]36 }3738 const mixedData = mix(data)3940 const res = [1000, 2000, 3000]41 .map(index => findValueAt(index, mixedData))42 .reduce((acc, el) => acc + el, 0)4344 console.log(res)45}4647main()
Part 2
Like part 1, but add decryptionKey
and times
Implementation
1const fs = require("fs")23const readData = () => {4 const data = fs.readFileSync("./input", "utf-8").split(/\r?\n/).map(Number)56 return data7}89const main = () => {10 const data = readData()1112 const mix = ({ arr, decryptionKey = 1, times = 1 }) => {13 const arrLength = arr.length14 const refs = arr.map(val => [val * decryptionKey])15 const original = [...refs]1617 while (times--) {18 for (const currentRef of original) {19 const value = currentRef[0]20 const isNegative = value < 021 if (isNegative) refs.reverse()22 const currentIndex = refs.findIndex(ref => ref === currentRef)23 const newIndex = (currentIndex + Math.abs(value)) % (arrLength - 1)24 refs.splice(currentIndex, 1)25 refs.splice(newIndex, 0, currentRef)26 if (isNegative) refs.reverse()27 }28 }2930 return refs.flat()31 }3233 const findValueAt = (index, arr) => {34 const arrLength = arr.length35 const indexOfZero = arr.findIndex(val => val === 0)36 index = (indexOfZero + index) % arrLength37 return arr[index]38 }3940 const mixedData = mix({ arr: data, decryptionKey: 811589153, times: 10 })4142 const res = [1000, 2000, 3000]43 .map(index => findValueAt(index, mixedData))44 .reduce((acc, el) => acc + el, 0)4546 console.log(res)47}4849main()
References
Comments
Loading comments...
Tags
adventofcode
array
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Floyd-Warshal and bitmask
Previous Post
Flooding with breadth-first search