Skip to content

Instantly share code, notes, and snippets.

@Mellen
Last active December 10, 2019 21:33
Show Gist options
  • Save Mellen/a5e361488a5802eda9622e1490b1c3a9 to your computer and use it in GitHub Desktop.
Save Mellen/a5e361488a5802eda9622e1490b1c3a9 to your computer and use it in GitHub Desktop.
AOC day 9
function Processor(instructions)
{
this.instructions = instructions.map(i => i);
this.instructionPointer = 0;
this.input = [];
this.inputPointer = 0;
this.output = null;
this.running = false;
this.sendOutput = null;
this.relativebase = 0;
this.opToLength =
{
'01': 4,
'02': 4,
'03': 2,
'04': 2,
'05': 3,
'06': 3,
'07': 4,
'08': 4,
'09': 2,
'99': 1
};
this.opToFunc =
{
'01': (modes, a, b, p) =>
{
a = this.modeSwitch(a, modes[0]);
b = this.modeSwitch(b, modes[1]);
if(modes[2] == 2)
{
p = this.relativebase + p;
}
this.instructions[p] = (a+b).toString();
},
'02': (modes, a, b, p) =>
{
a = this.modeSwitch(a, modes[0]);
b = this.modeSwitch(b, modes[1]);
if(modes[2] == 2)
{
p = this.relativebase + p;
}
this.instructions[p] = (a*b).toString();
},
'03': (modes, p, _, __) =>
{
if(modes[0] == 2)
{
p = this.relativebase + p;
}
this.instructions[p] = this.input[this.inputPointer].toString();
this.inputPointer++;
},
'04': (modes, p, _, __) =>
{
if(modes[0] == 2)
{
p = this.relativebase + p;
}
this.output = this.instructions[p];
},
'05': (modes, a, p, _) =>
{
a = this.modeSwitch(a, modes[0]);
p = this.modeSwitch(p, modes[1]);
if(a !== 0)
{
this.instructionPointer = p;
this.opToLength['05'] = 0;
}
else
{
this.opToLength['05'] = 3;
}
},
'06': (modes, a, p, _) =>
{
a = this.modeSwitch(a, modes[0]);
p = this.modeSwitch(p, modes[1]);
if(a === 0)
{
this.instructionPointer = p;
this.opToLength['06'] = 0;
}
else
{
this.opToLength['06'] = 3;
}
},
'07': (modes, a, b, p) =>
{
a = this.modeSwitch(a, modes[0]);
b = this.modeSwitch(b, modes[1]);
if(modes[2] == 2)
{
p = this.relativebase + p;
}
this.instructions[p] = a < b ? '1' : '0';
},
'08': (modes, a, b, p) =>
{
a = this.modeSwitch(a, modes[0]);
b = this.modeSwitch(b, modes[1]);
if(modes[2] == 2)
{
p = this.relativebase + p;
}
this.instructions[p] = a === b ? '1' : '0';
},
'09': (modes, a, _, __) =>
{
a = this.modeSwitch(a, modes[0]);
this.relativebase += a;
},
'99': (_,__,___,____) =>
{
this.running = false;
}
};
}
Processor.prototype.accessMemory = function(index)
{
let value = this.instructions[index];
if(typeof(value) === 'undefined')
{
value = '0';
this.instructions[index] = '0';
}
return value;
};
Processor.prototype.modeSwitch = function(value, mode)
{
if(mode == 0)
{
return parseInt(this.accessMemory(value));
}
if(mode == 2)
{
return parseInt(this.accessMemory(this.relativebase + value));
}
return value;
};
Processor.prototype.step = function()
{
let inst = this.accessMemory(this.instructionPointer).padStart(5, '0');
let opcode = inst.slice(-2);
let parameterModes = inst.substr(0,3).split('').reverse();
let a = parseInt(this.accessMemory(this.instructionPointer+1));
let b = parseInt(this.accessMemory(this.instructionPointer+2));
let p = parseInt(this.accessMemory(this.instructionPointer+3));
if(typeof(this.opToFunc[opcode]) === 'undefined')
{
console.log('error: inst', inst, 'op:',opcode, 'a', a, 'b', b, 'p', p, 'index', this.instructionPointer);
}
this.opToFunc[opcode](parameterModes, a, b, p);
let instructionInc = this.opToLength[opcode];
this.instructionPointer += instructionInc;
};
Processor.prototype.start = function()
{
this.running = true;
};
Processor.prototype.takeOutput = function()
{
let result = this.output;
this.output = null;
return result;
};
Processor.prototype.addInput = function(value)
{
this.input.push(value);
};
function testPart1()
{
let ins = '109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99'.split(',');
let p = new Processor(ins);
p.start();
while(p.running)
{
p.step();
if(p.output!=null)
{
console.log(p.takeOutput());
}
}
}
function part1()
{
let ins = document.querySelector('pre').innerHTML.split(',');
let p = new Processor(ins);
p.addInput(1);
p.start();
while(p.running)
{
p.step();
if(p.output!=null)
{
console.log(p.takeOutput());
}
}
}
function part2()
{
let ins = document.querySelector('pre').innerHTML.split(',');
let p = new Processor(ins);
p.addInput(2);
p.start();
while(p.running)
{
p.step();
if(p.output!=null)
{
console.log(p.takeOutput());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment