Public
Edited
Dec 7, 2022
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
splitInput = {
const [stacks, instructions] = input.split(emptyLine)

return {stacks, instructions}
}
Insert cell
stacks = parseStacks(splitInput.stacks)
Insert cell
instructions = R.map(parseInstruction, splitInput.instructions.split('\n'))
Insert cell
applyInstruction = (stacks, {count, from, to}) => {
R.range(0, count).forEach(() => {
stacks[to - 1].push(stacks[from - 1].pop())
})

return stacks
}
Insert cell
reorder = f => R.reduce(f, R.clone(stacks), instructions)
Insert cell
getTop = stacks => stacks.map(R.last).join('')
Insert cell
finalTopMost = getTop(reorder(applyInstruction))
Insert cell
Insert cell
applyInstruction9001 = (stacks, {count, from, to}) => {
const source = stacks[from - 1]
const [remaining, moved] = R.splitAt(source.length - count, source)

stacks[from - 1] = remaining
stacks[to - 1].push(...moved)

return stacks
}
Insert cell
finalTopMost9001 = getTop(reorder(applyInstruction9001))
Insert cell
Insert cell
emptyLine = /\n\s*\n/
Insert cell
parseStacks = text => {
const lines = text.split(/\n/).filter(x => x.startsWith('['))

const stacks = lines.map(line => line.replace(/ /g, ' []').split(/\s/).map(crate => crate.replace(/[\[\]]/g, '')))

return R.map(R.pipe(R.reverse, R.filter(R.identity)), R.transpose(stacks))
}
Insert cell
instructionRegex = /move (?<count>\d+) from (?<from>\d+) to (?<to>\d+)/
Insert cell
parseInstruction = line => R.map(parseInt, instructionRegex.exec(line).groups)
Insert cell
Insert cell
R = import('ramda')
Insert cell
import {adventOfCodeHeader, renderInput} from '@senritsu/advent-of-code-2022'
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more