Last active
December 6, 2019 11:21
-
-
Save CharlotteGore/69f136e0520b39fca490c3e69ed5d28c to your computer and use it in GitHub Desktop.
day4 aoc2019
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
var run = input => { | |
const program = new Int32Array(`3,225,1,225,6,6,1100,1,238,225,104,0,101,14,135,224,101,-69,224,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,102,90,169,224,1001,224,-4590,224,4,224,1002,223,8,223,1001,224,1,224,1,224,223,223,1102,90,45,224,1001,224,-4050,224,4,224,102,8,223,223,101,5,224,224,1,224,223,223,1001,144,32,224,101,-72,224,224,4,224,102,8,223,223,101,3,224,224,1,223,224,223,1102,36,93,225,1101,88,52,225,1002,102,38,224,101,-3534,224,224,4,224,102,8,223,223,101,4,224,224,1,223,224,223,1102,15,57,225,1102,55,49,225,1102,11,33,225,1101,56,40,225,1,131,105,224,101,-103,224,224,4,224,102,8,223,223,1001,224,2,224,1,224,223,223,1102,51,39,225,1101,45,90,225,2,173,139,224,101,-495,224,224,4,224,1002,223,8,223,1001,224,5,224,1,223,224,223,1101,68,86,224,1001,224,-154,224,4,224,102,8,223,223,1001,224,1,224,1,224,223,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,108,226,677,224,1002,223,2,223,1006,224,329,1001,223,1,223,1007,226,226,224,1002,223,2,223,1006,224,344,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,359,1001,223,1,223,107,226,677,224,1002,223,2,223,1005,224,374,101,1,223,223,1107,677,226,224,102,2,223,223,1006,224,389,101,1,223,223,108,677,677,224,102,2,223,223,1006,224,404,1001,223,1,223,1108,677,226,224,102,2,223,223,1005,224,419,101,1,223,223,1007,677,226,224,1002,223,2,223,1006,224,434,101,1,223,223,1107,226,226,224,1002,223,2,223,1006,224,449,101,1,223,223,8,677,226,224,102,2,223,223,1006,224,464,1001,223,1,223,1107,226,677,224,102,2,223,223,1005,224,479,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,494,1001,223,1,223,1108,677,677,224,102,2,223,223,1006,224,509,101,1,223,223,1008,677,677,224,102,2,223,223,1005,224,524,1001,223,1,223,107,226,226,224,1002,223,2,223,1005,224,539,101,1,223,223,7,226,226,224,102,2,223,223,1005,224,554,101,1,223,223,1108,226,677,224,1002,223,2,223,1006,224,569,1001,223,1,223,107,677,677,224,102,2,223,223,1005,224,584,101,1,223,223,7,677,226,224,1002,223,2,223,1005,224,599,101,1,223,223,108,226,226,224,1002,223,2,223,1005,224,614,101,1,223,223,1008,677,226,224,1002,223,2,223,1005,224,629,1001,223,1,223,7,226,677,224,102,2,223,223,1005,224,644,101,1,223,223,8,677,677,224,102,2,223,223,1005,224,659,1001,223,1,223,8,226,677,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226`.split(/,/g).map(m => parseInt(m))); | |
let pc = 0; | |
//let results = []; | |
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')}` | |
} | |
while(program[pc] !== 99 && !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: { | |
program[getPointer(3)] = getValue(1) + getValue(2); | |
pc += 3; | |
break; | |
} | |
case 2: { | |
program[getPointer(3)] = getValue(1) * getValue(2); | |
pc += 3; | |
break; | |
} | |
case 3: { | |
program[getPointer(1)] = input; | |
pc += 1 | |
break; | |
} | |
case 4: { | |
let r = getValue(1); | |
if (r !== 0){ | |
console.warn('Diagnostic code',r); | |
} | |
pc ++ | |
break; | |
} | |
case 5: { | |
if (getValue(1)) { | |
pc = getValue(2); | |
} else { | |
pc += 2; | |
} | |
break; | |
} | |
case 6: { | |
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; | |
} | |
pc += 3; | |
break; | |
} | |
case 8: { | |
let c = getPointer(3); | |
if (getValue(1) === getValue(2)) { | |
program[c] = 1; | |
} else { | |
program[c] = 0; | |
} | |
pc += 3; | |
break; | |
} | |
default: { | |
debugger; | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment