Advent of Code 2022 - Day 5: Supply Stacks Solution
Transform (the hard part) and mutatePart 1
Transform the raw data
1[D]2[N] [C]3[Z] [M] [P]4 1 2 356move 1 from 2 to 17move 3 from 1 to 38move 2 from 2 to 19move 1 from 1 to 2
into the following format
1{2 stackData: [ [ 'Z', 'N' ], [ 'M', 'C', 'D' ], [ 'P' ] ],3 moveData: [4 { numberOfItemsToMove: 1, srcStack: 1, destStack: 0 },5 { numberOfItemsToMove: 3, srcStack: 0, destStack: 2 },6 { numberOfItemsToMove: 2, srcStack: 1, destStack: 0 },7 { numberOfItemsToMove: 1, srcStack: 0, destStack: 1 }8 ]9}
With every move
- Grab
itemsToMove
fromsrcStack
- Reverse it
- Push to
destStack
Implementation
1const fs = require("fs")23const transpose = matrix => {4 return matrix[0].map((_, colIdx) =>5 matrix6 .map(row => row[colIdx])7 .reverse()8 .filter(Boolean)9 )10}1112const readData = () => {13 const data = fs.readFileSync("./sample-input", "utf-8").split(/\r?\n/)1415 let rawStackData = []1617 let i = 01819 while (data[i] !== "") {20 rawStackData.push(data[i++].split("").map(char => char.replace(" ", "")))21 }22 rawStackData.pop() // remove emply line2324 // take N as the numberOfStacks25 // total character in one line is:26 // each item in the stack, for example, "[X]" takes 3 characters27 // N-1 spaces between items28 // in total, we have 3N + (N-1) = 4N - 1 characters29 const numberOfStacks = (rawStackData[0].length + 1) / 43031 // [ Z ] S [ M ] S [ P ] (S means "space")32 // 0 1 2 3 4 5 6 7 8 9 1033 // item indexes will be 1 5 934 // which are even-index element of odd numbers (1, 3, 5, 7, 9,..)35 let itemIndexes = Array.from(36 { length: rawStackData[0].length },37 (_, i) => 2 * i + 138 )39 .filter((_, i) => i % 2 === 0)40 .slice(0, numberOfStacks)4142 let stackData = transpose(43 rawStackData.map(data => itemIndexes.map(i => data[i]))44 )4546 let moveData = data47 .slice(i)48 .filter(Boolean)49 .map(moveStatement =>50 moveStatement51 .replace(/move (\d+) from (\d+) to (\d+)/g, "$1 $2 $3")52 .split(" ")53 .map(Number)54 )55 .map(([numberOfItemsToMove, srcStack, destStack]) => ({56 numberOfItemsToMove,57 srcStack: srcStack - 1,58 destStack: destStack - 1,59 }))6061 return [stackData, moveData]62}6364const main = () => {65 const [stackData, moveData] = readData()6667 console.log({ stackData, moveData })6869 for (const { numberOfItemsToMove, srcStack, destStack } of moveData) {70 const itemsToMove = stackData[srcStack].slice(-numberOfItemsToMove)71 stackData[srcStack] = stackData[srcStack].slice(0, -numberOfItemsToMove)72 stackData[destStack] = [...stackData[destStack], ...itemsToMove.reverse()]73 }7475 const res = stackData.map(data => data.slice(-1)[0]).join("")7677 console.log(res)78}7980main()
Part 2
Similar to Part 1, but no need to reverse itemsToMove
Implementation
1const fs = require("fs")23const transpose = matrix => {4 return matrix[0].map((_, colIdx) =>5 matrix6 .map(row => row[colIdx])7 .reverse()8 .filter(Boolean)9 )10}1112const readData = () => {13 const data = fs.readFileSync("./sample-input", "utf-8").split(/\r?\n/)1415 let rawStackData = []1617 let i = 01819 while (data[i] !== "") {20 rawStackData.push(data[i++].split("").map(char => char.replace(" ", "")))21 }22 rawStackData.pop() // remove emply line2324 // take N as the numberOfStacks25 // total character in one line is:26 // each item in the stack, for example, "[X]" takes 3 characters27 // N-1 spaces between items28 // in total, we have 3N + (N-1) = 4N - 1 characters29 const numberOfStacks = (rawStackData[0].length + 1) / 43031 // [ Z ] S [ M ] S [ P ] (S means "space")32 // 0 1 2 3 4 5 6 7 8 9 1033 // item indexes will be 1 5 934 // which are even-index element of odd numbers (1, 3, 5, 7, 9,..)35 let itemIndexes = Array.from(36 { length: rawStackData[0].length },37 (_, i) => 2 * i + 138 )39 .filter((_, i) => i % 2 === 0)40 .slice(0, numberOfStacks)4142 let stackData = transpose(43 rawStackData.map(data => itemIndexes.map(i => data[i]))44 )4546 let moveData = data47 .slice(i)48 .filter(Boolean)49 .map(moveStatement =>50 moveStatement51 .replace(/move (\d+) from (\d+) to (\d+)/g, "$1 $2 $3")52 .split(" ")53 .map(Number)54 )55 .map(([numberOfItemsToMove, srcStack, destStack]) => ({56 numberOfItemsToMove,57 srcStack: srcStack - 1,58 destStack: destStack - 1,59 }))6061 return [stackData, moveData]62}6364const main = () => {65 const [stackData, moveData] = readData()6667 console.log({ stackData, moveData })6869 for (const { numberOfItemsToMove, srcStack, destStack } of moveData) {70 const itemsToMove = stackData[srcStack].slice(-numberOfItemsToMove)71 stackData[srcStack] = stackData[srcStack].slice(0, -numberOfItemsToMove)72 stackData[destStack] = [...stackData[destStack], ...itemsToMove]73 }7475 const res = stackData.map(data => data.slice(-1)[0]).join("")7677 console.log(res)78}7980main()
References
Comments
Loading comments...
Tags
adventofcode
stack
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
Advent of Code 2022 - Day 6: Tuning Trouble
Use a Set to check if all is different
Previous Post
Advent of Code 2022 - Day 4: Camp Cleanup
Dealing with start, end