You're absolutely right. I've changed the approach and with this I managed to reduce the processing time from 43.3 seconds to 2.6:
const calculateOrbits = (input, centre, orbits=0) => { const objects = input.filter(x => x.split(')')[0] == centre) return objects. length == 0 ? orbits : objects.reduce((a, b) => a + calculateOrbits(input, b.split(')')[1], orbits + 1), orbits) } const calculateTransfers = (input, origin, destination) => { const fromYou = getOrbitChain(input, origin) const fromDest = getOrbitChain(input, destination) const commonObject = fromYou.filter(x => fromDest.includes(x))[0] return fromYou.indexOf(commonObject) + fromDest.indexOf(commonObject) - 2 } const findElement = (input, tag) => input.filter(x => x.split(')')[1] == tag) const getOrbitChain = (input, element, chain=[]) => { const [ centre, object ] = element.split(')') const newChain = chain.concat(centre) if (centre == 'COM') return newChain const [ nextObject ] = findElement(input, centre) return getOrbitChain(input, nextObject, newChain) } module.exports = input => { const data = input.split('\n') const partOne = calculateOrbits(data, 'COM') const partTwo = calculateTransfers(data, 'YOU', 'SAN') return({ partOne, partTwo }) }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You're absolutely right. I've changed the approach and with this I managed to reduce the processing time from 43.3 seconds to 2.6: