advent of code 2023 day 8: replace forEach and {} with reduce
This commit is contained in:
parent
c3684c23b1
commit
b2f160e760
|
@ -2,12 +2,11 @@
|
|||
module.exports.solve = function(input) {
|
||||
const [instructionLine, nodeLines] = input.split('\n\n')
|
||||
const instructions = instructionLine.split('').map(s => s === 'R' ? 1 : 0)
|
||||
const map = {} // this could be a reduce()?
|
||||
const nodes = nodeLines.split('\n').forEach(line => {
|
||||
const map = nodeLines.split('\n').reduce((acc, line) => {
|
||||
// e.g. AAA = (BBB, CCC)
|
||||
const [pos, _, left, right] = line.replace(/[\(]?[\)]?[=]?[,]?/g, "").split(' ')
|
||||
map[pos] = [left, right]
|
||||
})
|
||||
return acc[pos] = [left, right], acc
|
||||
}, {})
|
||||
|
||||
let curr = 'AAA'
|
||||
let i = 0
|
||||
|
|
|
@ -15,8 +15,7 @@ EEE = (EEE, EEE)
|
|||
GGG = (GGG, GGG)
|
||||
ZZZ = (ZZZ, ZZZ)`
|
||||
|
||||
const out = solve(input)
|
||||
assert.equal(out, 2)
|
||||
assert.equal(solve(input), 2)
|
||||
})
|
||||
|
||||
|
||||
|
@ -27,6 +26,5 @@ AAA = (BBB, BBB)
|
|||
BBB = (AAA, ZZZ)
|
||||
ZZZ = (ZZZ, ZZZ)`
|
||||
|
||||
const out = solve(input)
|
||||
assert.equal(out, 6)
|
||||
assert.equal(solve(input), 6)
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue