Today's code is a lot uglier than yesterday's, but, oh well, it works. My JavaScript solution:
const opcodes = { 1: { func: (intcode, posA, posB, posC, modA=0, modB=0) => intcode[posC] = applyMode(intcode, posA, modA) + applyMode(intcode, posB, modB), next: 4, }, 2: { func: (intcode, posA, posB, posC, modA=0, modB=0) => intcode[posC] = applyMode(intcode, posA, modA) * applyMode(intcode, posB, modB), next: 4, }, 3: { func: (intcode, pos, input) => intcode[pos] = input, next: 2, }, 4: { func: (intcode, pos, _, __, mod=0) => applyMode(intcode, pos, mod), next: 2, }, 5: { func: (intcode, posA, posB, _, modA=0, modB=0, position) => opcodes[5].next = applyMode(intcode, posA, modA) != 0 ? applyMode(intcode, posB, modB) - position : 3, next: 3 }, 6: { func: (intcode, posA, posB, _, modA=0, modB=0, position) => opcodes[6].next = applyMode(intcode, posA, modA) == 0 ? applyMode(intcode, posB, modB) - position : 3, next: 3, }, 7: { func: (intcode, posA, posB, posC, modA=0, modB=0) => intcode[posC] = applyMode(intcode, posA, modA) < applyMode(intcode, posB, modB) ? 1 : 0, next: 4, }, 8: { func: (intcode, posA, posB, posC, modA=0, modB=0) => intcode[posC] = applyMode(intcode, posA, modA) == applyMode(intcode, posB, modB) ? 1 : 0, next: 4, }, } const applyMode = (intcode, pos, mode) => mode == 0 ? intcode[pos] : pos const computeOpcode = (intcode, input, position=0, diagnosticCode) => { if (intcode[position] == 99) return diagnosticCode const [ oc, _, m1, m2, m3 ] = intcode[position].toString().split('').reverse().map(x => +x) const [ a, b, dest ] = intcode.slice(position + 1) const bInput = oc == 3 ? input : b diagnosticCode = opcodes[oc].func(intcode, a, bInput, dest, m1, m2, position) if (oc == 4) console.log(diagnosticCode) return computeOpcode(intcode, input, position + opcodes[oc].next, diagnosticCode) } module.exports = input => { const data = input.split(',').map(x => +x) const dataOne = Object.assign([], data) const dataTwo = Object.assign([], data) const partOne = computeOpcode(dataOne, 1) console.log('Part Two') const partTwo = computeOpcode(dataTwo, 5) 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.
Today's code is a lot uglier than yesterday's, but, oh well, it works. My JavaScript solution: