Published
Edited
Dec 4, 2019
Importers
Insert cell
Insert cell
Insert cell
testprograms = ["1,9,10,3,2,3,11,0,99,30,40,50","1,0,0,0,99","2,3,0,3,99","2,4,4,5,99,0","1,1,1,4,99,5,6,0,99"]
Insert cell
formatData = x => x.split(",").map(Number)
Insert cell
run = x =>
{
let program = x.slice(0)
for(let i = 0; ; i+=4) {
if(program[i] == 1) {
program[program[i+3]] = program[program[i+1]] + program[program[i+2]]
} else if(program[i] == 2){
program[program[i+3]] = program[program[i+1]] * program[program[i+2]]
} else if(program[i] == 99) {
break
}
}
return program
}
Insert cell
testprograms
.map(formatData)
.map(run)
Insert cell
inputs = xs => (noun,verb) => {xs[1] = noun; xs[2] = verb; return xs}
Insert cell
solve1 = d => run(inputs(formatData(d))(12,2))[0]
Insert cell
solve1(data)
Insert cell
Insert cell
solve2 = d =>
{
const inputsCurried = inputs(formatData(d))

let noun, verb
bruteforceloops:
for(noun = 0; noun < 100; noun++) {
for(verb = 0; verb < 100; verb++) {
if(run(inputsCurried(noun,verb))[0]==19690720) break bruteforceloops
}
}
return 100 * noun + verb
}
Insert cell
solve2(data)
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