Published
Edited
Dec 9, 2019
Insert cell
md`# Advent of Code 2019 Day 5`
Insert cell
{
const op = 1101;
return [
op % 100,
Math.floor( op / 100 ) % 10,
Math.floor( op / 1000 ) % 10,
Math.floor( op / 10000 ) % 10
];
}
Insert cell
function intcode( origProgram, input = [] ) {
const program = [...origProgram];
const output = [];
let pointer = 0;
while ( program[ pointer ] !== 99 ) {
let op = program[ pointer ];
let args = [ program[ program[ pointer + 1 ] ], program[ program[ pointer + 2 ] ], program[ program[ pointer + 3 ] ] ];
// Immediate mode
if ( op > 99 ) {
if ( Math.floor( op / 100 ) % 10 == 1 ) args[0] = program[ pointer + 1 ];
if ( Math.floor( op / 1000 ) % 10 == 1 ) args[1] = program[ pointer + 2 ];
if ( Math.floor( op / 10000 ) % 10 == 1 ) args[2] = program[ pointer + 3 ];
op = op % 100;
}
console.log( { op, args } );
switch ( op ) {
case 1:
program[ program[ pointer + 3 ] ] = args[0] + args[1];
pointer += 4;
break;
case 2:
program[ program[ pointer + 3 ] ] = args[0] * args[1];
pointer += 4;
break;
case 3:
program[ program[ pointer + 1 ] ] = input.shift();
pointer += 2;
break;
case 4:
output.push( args[ 0 ] );
pointer += 2;
break;
case 5:
if ( args[0] !== 0 ) {
pointer = args[1];
} else {
pointer += 3;
}
break;
case 6:
if ( args[0] == 0 ) {
pointer = args[1];
} else {
pointer += 3;
}
break;
case 7:
program[ program[ pointer + 3 ] ] = ( args[0] < args[1] ) ? 1 : 0;
pointer += 4;
break;
case 8:
program[ program[ pointer + 3 ] ] = ( args[0] == args[1] ) ? 1 : 0;
pointer += 4;
break;
default:
throw `Bad operation: ${op}`
}
}
return { program, output };
}
Insert cell
intcode( [3,0,4,0,99], [100] ).output[0]
Insert cell
intcode( [ 1,9,10,3,2,3,11,0,99,30,40,50 ] ).program[0]
Insert cell
intcode( [ 1101, 100, -1, 4, 0 ] ).program[0]
Insert cell
md`## Part 1:`
Insert cell
{
const { output } = intcode( [ 3,225,1,225,6,6,1100,1,238,225,104,0,101,71,150,224,101,-123,224,224,4,224,102,8,223,223,101,2,224,224,1,224,223,223,2,205,209,224,1001,224,-3403,224,4,224,1002,223,8,223,101,1,224,224,1,223,224,223,1101,55,24,224,1001,224,-79,224,4,224,1002,223,8,223,101,1,224,224,1,223,224,223,1,153,218,224,1001,224,-109,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1002,201,72,224,1001,224,-2088,224,4,224,102,8,223,223,101,3,224,224,1,223,224,223,1102,70,29,225,102,5,214,224,101,-250,224,224,4,224,1002,223,8,223,1001,224,3,224,1,223,224,223,1101,12,52,225,1101,60,71,225,1001,123,41,224,1001,224,-111,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223,1102,78,66,224,1001,224,-5148,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,1101,29,77,225,1102,41,67,225,1102,83,32,225,1101,93,50,225,1102,53,49,225,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,1107,677,677,224,1002,223,2,223,1005,224,329,101,1,223,223,7,677,677,224,1002,223,2,223,1005,224,344,1001,223,1,223,7,226,677,224,102,2,223,223,1006,224,359,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,374,1001,223,1,223,8,226,677,224,1002,223,2,223,1006,224,389,1001,223,1,223,1108,226,677,224,1002,223,2,223,1006,224,404,101,1,223,223,1107,677,226,224,102,2,223,223,1006,224,419,101,1,223,223,1007,677,677,224,1002,223,2,223,1005,224,434,101,1,223,223,7,677,226,224,102,2,223,223,1006,224,449,1001,223,1,223,1008,226,677,224,1002,223,2,223,1006,224,464,101,1,223,223,8,677,677,224,1002,223,2,223,1006,224,479,101,1,223,223,108,226,226,224,102,2,223,223,1005,224,494,101,1,223,223,1107,226,677,224,1002,223,2,223,1006,224,509,101,1,223,223,107,226,226,224,1002,223,2,223,1006,224,524,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,539,101,1,223,223,1007,226,226,224,102,2,223,223,1006,224,554,101,1,223,223,108,677,677,224,102,2,223,223,1005,224,569,101,1,223,223,107,677,226,224,102,2,223,223,1005,224,584,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,599,101,1,223,223,1108,677,226,224,1002,223,2,223,1006,224,614,101,1,223,223,8,677,226,224,102,2,223,223,1005,224,629,1001,223,1,223,1008,677,677,224,102,2,223,223,1006,224,644,101,1,223,223,1007,226,677,224,102,2,223,223,1005,224,659,101,1,223,223,108,226,677,224,102,2,223,223,1006,224,674,101,1,223,223,4,223,99,226], [ 1 ] );
return output[ output.length - 1 ];
}
Insert cell
md`# Part 2:`
Insert cell
// Using position mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
intcode( [ 3,9,8,9,10,9,4,9,99,-1,8 ], [ 8 ] ).output
Insert cell
// Using position mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
intcode( [ 3,9,7,9,10,9,4,9,99,-1,8 ], [ 40 ] ).output
Insert cell
// 3,3,1108,-1,8,3,4,3,99 - Using immediate mode, consider whether the input is equal to 8; output 1 (if it is) or 0 (if it is not).
intcode( [ 3,3,1108,-1,8,3,4,3,99 ], [ 8 ] ).output
Insert cell
// Using immediate mode, consider whether the input is less than 8; output 1 (if it is) or 0 (if it is not).
intcode( [ 3,3,1107,-1,8,3,4,3,99 ], [ 7 ] ).output
Insert cell
// Here are some jump tests that take an input, then output 0 if the input was zero or 1 if the input was non-zero:
intcode( [ 3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9 ], [ 5 ] ).output
Insert cell
// Here are some jump tests that take an input, then output 0 if the input was zero or 1 if the input was non-zero:
intcode( [ 3,3,1105,-1,9,1101,0,0,12,4,12,99,1 ], [ 0 ] ).output
Insert cell
intcode( [ 3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99 ], [ 10 ]).output

Insert cell
{
const { output } = intcode( [ 3,225,1,225,6,6,1100,1,238,225,104,0,101,71,150,224,101,-123,224,224,4,224,102,8,223,223,101,2,224,224,1,224,223,223,2,205,209,224,1001,224,-3403,224,4,224,1002,223,8,223,101,1,224,224,1,223,224,223,1101,55,24,224,1001,224,-79,224,4,224,1002,223,8,223,101,1,224,224,1,223,224,223,1,153,218,224,1001,224,-109,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1002,201,72,224,1001,224,-2088,224,4,224,102,8,223,223,101,3,224,224,1,223,224,223,1102,70,29,225,102,5,214,224,101,-250,224,224,4,224,1002,223,8,223,1001,224,3,224,1,223,224,223,1101,12,52,225,1101,60,71,225,1001,123,41,224,1001,224,-111,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223,1102,78,66,224,1001,224,-5148,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,1101,29,77,225,1102,41,67,225,1102,83,32,225,1101,93,50,225,1102,53,49,225,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,1107,677,677,224,1002,223,2,223,1005,224,329,101,1,223,223,7,677,677,224,1002,223,2,223,1005,224,344,1001,223,1,223,7,226,677,224,102,2,223,223,1006,224,359,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,374,1001,223,1,223,8,226,677,224,1002,223,2,223,1006,224,389,1001,223,1,223,1108,226,677,224,1002,223,2,223,1006,224,404,101,1,223,223,1107,677,226,224,102,2,223,223,1006,224,419,101,1,223,223,1007,677,677,224,1002,223,2,223,1005,224,434,101,1,223,223,7,677,226,224,102,2,223,223,1006,224,449,1001,223,1,223,1008,226,677,224,1002,223,2,223,1006,224,464,101,1,223,223,8,677,677,224,1002,223,2,223,1006,224,479,101,1,223,223,108,226,226,224,102,2,223,223,1005,224,494,101,1,223,223,1107,226,677,224,1002,223,2,223,1006,224,509,101,1,223,223,107,226,226,224,1002,223,2,223,1006,224,524,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,539,101,1,223,223,1007,226,226,224,102,2,223,223,1006,224,554,101,1,223,223,108,677,677,224,102,2,223,223,1005,224,569,101,1,223,223,107,677,226,224,102,2,223,223,1005,224,584,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,599,101,1,223,223,1108,677,226,224,1002,223,2,223,1006,224,614,101,1,223,223,8,677,226,224,102,2,223,223,1005,224,629,1001,223,1,223,1008,677,677,224,102,2,223,223,1006,224,644,101,1,223,223,1007,226,677,224,102,2,223,223,1005,224,659,101,1,223,223,108,226,677,224,102,2,223,223,1006,224,674,101,1,223,223,4,223,99,226], [ 5 ] );
return output;
}
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