74 lines
2.0 KiB
JavaScript
74 lines
2.0 KiB
JavaScript
|
generateSequence = (avalue)=>{
|
||
|
var instructions = [];
|
||
|
var registers = {a: avalue, b: 0, c: 0, d: 0};
|
||
|
var output = [];
|
||
|
|
||
|
var instructionSet = {};
|
||
|
|
||
|
instructionSet.cpy = (a, b)=>{
|
||
|
if(Number.isInteger(parseInt(a))){
|
||
|
registers[b] = parseInt(a);
|
||
|
}
|
||
|
else{
|
||
|
registers[b] = registers[a];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
instructionSet.inc = (a)=>{
|
||
|
registers[a]++;
|
||
|
}
|
||
|
|
||
|
instructionSet.dec = (a)=>{
|
||
|
registers[a]--;
|
||
|
}
|
||
|
|
||
|
instructionSet.jnz = (a, b)=>{
|
||
|
if(((registers[a] != 0) && (a in registers)) || ((parseInt(a) != 0) && (!isNaN(parseInt(a))))){
|
||
|
pc = pc + (parseInt(b) - 1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
instructionSet.out = (a)=>{
|
||
|
//console.log(registers);
|
||
|
output.push(registers[a]);
|
||
|
}
|
||
|
|
||
|
var input = require("fs").readFileSync("input.txt").toString().replace(/\r/g, "");
|
||
|
var instructions = input.split("\n").filter((a)=>(a)).map((instruction)=>{
|
||
|
if(instruction.indexOf("cpy") > -1){
|
||
|
var instructionParts = instruction.match("cpy ([^ ]+) ([^ ]+)");
|
||
|
return ["cpy", [instructionParts[1], instructionParts[2]]];
|
||
|
}
|
||
|
if(instruction.indexOf("inc") > -1){
|
||
|
var instructionParts = instruction.match("inc ([^ ]+)");
|
||
|
return ["inc", [instructionParts[1]]];
|
||
|
}
|
||
|
if(instruction.indexOf("dec") > -1){
|
||
|
var instructionParts = instruction.match("dec ([^ ]+)");
|
||
|
return ["dec", [instructionParts[1]]];
|
||
|
}
|
||
|
if(instruction.indexOf("out") > -1){
|
||
|
var instructionParts = instruction.match("out ([^ ]+)");
|
||
|
return ["out", [instructionParts[1]]];
|
||
|
}
|
||
|
if(instruction.indexOf("jnz") > -1){
|
||
|
var instructionParts = instruction.match("jnz ([^ ]+) ([^ ]+)");
|
||
|
return ["jnz", [instructionParts[1], instructionParts[2]]];
|
||
|
}
|
||
|
});
|
||
|
|
||
|
var pc = 0;
|
||
|
var i = 0;
|
||
|
while(i < 100000){
|
||
|
if(pc == 14){
|
||
|
console.log("14", registers);
|
||
|
}
|
||
|
instructionSet[instructions[pc][0]].apply(null, instructions[pc][1]);
|
||
|
pc++;
|
||
|
i++
|
||
|
}
|
||
|
console.log(registers);
|
||
|
return output;
|
||
|
}
|
||
|
console.log(generateSequence(196));
|