Published
Edited
Insert cell
Insert cell
function run (state) {
const {memory, pointer = 0, rbase = 0, input = [], outputs = [], halted = false} = state
let mem = memory.slice().split(",").map(Number)
const opcode = String(mem[pointer]).padStart(5,"0")
const command = +opcode.substring(3)
//base
if(command===99 || pointer >= mem.length - 1)
return {memory: mem.join(","), pointer: pointer+2,
input: input.slice(1), outputs: outputs, rbase: rbase, halted: true}
let param1 = +opcode[2]===0 ? mem[mem[pointer+1]] :
+opcode[2]===1 ? mem[pointer+1] : mem[mem[pointer+1]+rbase],
param2 = +opcode[1]===0 ? mem[mem[pointer+2]] :
+opcode[1]===1 ? mem[pointer+2] : mem[mem[pointer+2]+rbase],
param3 = +opcode[0]===0 ? mem[pointer+3] : mem[pointer+3]+rbase
//recursive
if(command===3) {
if(+opcode[2]==2) { // relative mode
mem[param3] = input[0]
} else {
mem[mem[pointer+1]] = input[0]
}
return run({memory: mem.join(","),
pointer: pointer+2,
input: input.slice(1), outputs: outputs, rbase: rbase,
halted: halted})
}
if(command===4)
return run({memory: mem.join(","),
pointer: pointer+2,
input: input, outputs: outputs.concat(param1), rbase: rbase,
halted: halted})
if(command==5)
return run({memory: mem.join(","),
pointer: param1!=0 ? param2 : pointer+3,
input: input, outputs: outputs, rbase: rbase,
halted: halted})
if(command==6)
return run({memory: mem.join(","),
pointer: param1==0? param2 : pointer+3,
input: input, outputs: outputs, rbase: rbase,
halted: halted})
if(command==1) mem[param3] = param1 + param2
if(command==2) mem[param3] = param1 * param2
if(command==7) mem[param3] = param1 < param2 ? 1 : 0
if(command==8) mem[param3] = param1 == param2 ? 1 : 0
if(command==9)
return run({memory: mem.join(","),
pointer: pointer+2, rbase: rbase + param1,
input: input, outputs: outputs, halted: halted})
return run({memory: mem.join(","), pointer: pointer+4,
rbase: rbase, input: input, outputs: outputs, halted: halted})
}
Insert cell
run({memory: day5data, input: [1]}) // verify on an old program
Insert cell
padStringRight = num => "," + Array.from({length: num}, () => 0).join()
Insert cell
quinine = run({memory: testprograms[0]+padStringRight(100)}) // example from problem description
Insert cell
solve = (program, _input) => run({memory: program+padStringRight(100), input: _input}).outputs.pop()
Insert cell
solve(data, [1]) // part 1
Insert cell
// part 2
// only works in safari? is safari the only browser offering tail call optimization?
solve(data, [2])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more