Created
December 7, 2019 08:19
-
-
Save CharlotteGore/0b9bd783ea2ae95da91af39284be121a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const permutations = arr => { | |
let perms = []; | |
for (let i = 0; i < arr.length; i++) { | |
let rest = permutations(arr.slice(0, i).concat(arr.slice(i + 1))); | |
if(!rest.length) { | |
perms.push([arr[i]]) | |
continue; | |
} | |
for(let j = 0; j < rest.length; j++) { | |
perms.push([arr[i]].concat(rest[j])) | |
} | |
} | |
return perms; | |
} | |
const amplifier = phase => { | |
const program = new Int32Array(`3,8,1001,8,10,8,105,1,0,0,21,34,47,72,81,102,183,264,345,426,99999,3,9,102,5,9,9,1001,9,3,9,4,9,99,3,9,101,4,9,9,1002,9,3,9,4,9,99,3,9,102,3,9,9,101,2,9,9,102,5,9,9,1001,9,3,9,1002,9,4,9,4,9,99,3,9,101,5,9,9,4,9,99,3,9,101,3,9,9,1002,9,5,9,101,4,9,9,102,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,99`.split(/,/g).map(m => parseInt(m))); | |
let pc = 0; | |
let results = []; | |
let inputIndex = 0; | |
const mode = { | |
1: 0, | |
2: 0, | |
3: 0 | |
} | |
const getValue = (d) => (mode[d] ? program[pc + (d - 1)] : program[program[pc + (d - 1)]]); | |
const getPointer = (d) => (mode[d] ? pc + (d - 1) : program[pc + (d - 1)]); | |
const modes = () => { | |
return `p1:${(mode[1] ? 'i' : 'p')} p2:${(mode[2] ? 'i' : 'p')} p3:${(mode[3] ? 'i' : 'p')}` | |
} | |
let usedPhase = false; | |
return (input) => { | |
while(!window.stopNowPlease){ | |
let inst = program[pc++]; | |
mode[3] = ~~(inst / 10000) | |
let op = inst - (mode[3] * 10000); | |
mode[2] = ~~(op / 1000); | |
op = op - (mode[2] * 1000); | |
mode[1] = ~~(op / 100) | |
op = op - (mode[1] * 100); | |
switch(op) { | |
case 1: { | |
let a = getValue(1); | |
let b = getValue(2); | |
program[getPointer(3)] = a + b; | |
//results.push(`add ${mode[1] ? '' : '$'}${program[pc]}(${a}) ${mode[2] ? '' : '$'}${program[pc+1]}(${b}) $${program[pc+2]}(${getValue(3)})`); | |
pc += 3; | |
break; | |
} | |
case 2: { | |
let a = getValue(1); | |
let b = getValue(2); | |
program[getPointer(3)] = a * b; | |
//results.push(`mul ${mode[1] ? '' : '$'}${program[pc]}(${a}) ${mode[2] ? '' : '$'}${program[pc+1]}(${b}) $${program[pc+2]}(${getValue(3)})`); | |
pc += 3; | |
break; | |
} | |
case 3: { | |
let i; | |
if (!usedPhase) { | |
i = phase; | |
usedPhase = !usedPhase; | |
} else { | |
i = input; | |
} | |
program[getPointer(1)] = i; | |
//results.push(`set ${i} ${mode[1] ? '' : '$'}${program[pc]}(${getValue(1)}) `); | |
pc += 1 | |
break; | |
} | |
case 4: { | |
let r = getValue(1); | |
//results.push(`get ${mode[1] ? '' : '$'}${program[pc]}(${r}) ${input}`) | |
pc++; | |
return r; | |
break; | |
} | |
case 5: { | |
if (getValue(1)) { | |
pc = getValue(2); | |
} else { | |
pc += 2; | |
} | |
//results.push(`jit ${mode[1] ? '' : '$'}${program[pc]}(${getValue(1)}) $${program[pc+1]}(${getValue(2)})`); | |
break; | |
} | |
case 6: { | |
//results.push(`jif ${mode[1] ? '' : '$'}${program[pc]}(${getValue(1)}) $${program[pc+1]}(${getValue(2)})`); | |
if (!getValue(1)) { | |
pc = getValue(2); | |
} else { | |
pc += 2; | |
} | |
break; | |
} | |
case 7: { | |
let c = getPointer(3); | |
if (getValue(1) < getValue(2)) { | |
program[c] = 1; | |
} else { | |
program[c] = 0; | |
} | |
//results.push(`jlt ${mode[1] ? '' : '$'}${program[pc]}(${getValue(1)}) ${mode[2] ? '' : '$'}${program[pc+1]}(${getValue(2)}) $${program[pc+2]}(${getValue(3)})`); | |
pc += 3; | |
break; | |
} | |
case 8: { | |
//results.push(`jeq ${mode[1] ? '' : '$'}${program[pc]}(${getValue(1)}) ${mode[2] ? '' : '$'}${program[pc+1]}(${getValue(2)}) $${program[pc+2]}(${getValue(3)})`); | |
let c = getPointer(3); | |
if (getValue(1) === getValue(2)) { | |
program[c] = 1; | |
} else { | |
program[c] = 0; | |
} | |
pc += 3; | |
break; | |
} | |
case 99: { | |
return null | |
} | |
default: { | |
debugger; | |
break; | |
} | |
} | |
} | |
return null; | |
} | |
} | |
const run () => { | |
const result = permutations([5, 6, 7, 8, 9]).map(perm => { | |
let amps = [ | |
amplifier(perm[0]), | |
amplifier(perm[1]), | |
amplifier(perm[2]), | |
amplifier(perm[3]), | |
amplifier(perm[4]), | |
]; | |
let i = 0; | |
let v = 0; | |
let done = false; | |
while(!done) { | |
let r = amps[i](v); | |
if (r !== null) { | |
v = r; | |
i = (i + 1) % amps.length; | |
} else { | |
done = true; | |
return { | |
score: v, | |
perm | |
} | |
} | |
} | |
}).sort((a, b) => a.score - b.score); | |
return result[result.length -1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment