Published
Edited
Dec 13, 2019
Insert cell
Insert cell
allValidPhaseSequences = (feedbackmode = false) => {
let result = []
let offset = feedbackmode ? 5 : 0
for(let i = 0+offset; i < 5+offset; i++) {
for(let j = 0+offset; j < 5+offset; j++) {
for(let k = 0+offset; k < 5+offset; k++) {
for(let l = 0+offset; l < 5+offset; l++) {
for(let p = 0+offset; p < 5+offset; p++) {
result.push([i,j,k,l,p])
}
}
}
}
}
return result.filter(z => new Set(z).size===5) // 0,1,2,3,4 in any order
}
Insert cell
function run (state) {
const {memory, pointer = 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, halted: true}
//recursive
if(command===3) {
mem[mem[pointer+1]] = input[0]
return run({memory: mem.join(","),
pointer: pointer+2,
input: input.slice(1), outputs: outputs,
halted: halted})
}
let param1 = +opcode[2]===0 ? mem[mem[pointer+1]] : mem[pointer+1],
param2 = +opcode[1]===0 ? mem[mem[pointer+2]] : mem[pointer+2]
if(command===4)
return {memory: mem.join(","),
pointer: pointer+2,
input: input, outputs: outputs.concat(param1),
halted: halted}
if(command==5)
return run({memory: mem.join(","),
pointer: param1!=0 ? param2 : pointer+3,
input: input, outputs: outputs,
halted: halted})
if(command==6)
return run({memory: mem.join(","),
pointer: param1==0? param2 : pointer+3,
input: input, outputs: outputs,
halted: halted})
if(command==1) mem[mem[pointer+3]] = param1 + param2
if(command==2) mem[mem[pointer+3]] = param1 * param2
if(command==7) mem[mem[pointer+3]] = param1 < param2 ? 1 : 0
if(command==8) mem[mem[pointer+3]] = param1 == param2 ? 1 : 0
return run({memory: mem.join(","), pointer: pointer+4, input: input, outputs: outputs, halted: halted})
}
Insert cell
thrustSignal = program => phaseSeq =>
{
let amps = phaseSeq
.map(phase => ({memory: program, input: [phase], halted: false}))
let signal = 0
let index = 0
for(let i = 0; ; i++) {
index = i % amps.length // make the index loop through 0-4 indefinitely
amps[index].input.push(signal)
amps[index] = run(amps[index])
signal = amps[index].outputs[amps[index].outputs.length-1]
if(index==4 && amps[index].halted) break
}
return amps[4].outputs[amps[4].outputs.length-1] // Amp E's final output
}
Insert cell
solve = (programdata, feedbackmode) =>
allValidPhaseSequences(feedbackmode)
.map(thrustSignal(programdata))
.reduce((acc, inc) => Math.max(acc,inc), -Infinity) // find largest of all signals
Insert cell
solve(programsTest2[0],phasesTest2[0]) // problem example
Insert cell
solve(data,false) // part 1
Insert cell
solve(data,true) // part 2
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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