This commit is contained in:
Tim Stallard 2016-12-24 21:06:34 +00:00
parent 0ed68f6338
commit 6c162bc7bc
3 changed files with 180 additions and 0 deletions

26
day23/input.txt Normal file
View File

@ -0,0 +1,26 @@
cpy a b
dec b
cpy a d
cpy 0 a
cpy b c
inc a
dec c
jnz c -2
dec d
jnz d -5
dec b
cpy b c
cpy c d
dec d
inc c
jnz d -2
tgl c
cpy -16 c
jnz 1 c
cpy 73 c
jnz 82 d
inc a
inc d
jnz d -2
inc c
jnz c -5

77
day23/part1.js Normal file
View File

@ -0,0 +1,77 @@
var instructions = [];
var registers = {a: 7, b: 0, c: 0, d: 0};
var instructionSet = {};
instructionSet.cpy = (a, b)=>{
if(b in registers){
if(Number.isInteger(parseInt(a))){
registers[b] = parseInt(a);
}
else{
registers[b] = registers[a];
}
}
}
instructionSet.inc = (a)=>{
if(a in registers){
registers[a]++;
}
}
instructionSet.dec = (a)=>{
if(a in registers){
registers[a]--;
}
}
instructionSet.jnz = (a, b)=>{
b = (Number.isInteger(parseInt(b))) ? parseInt(b) : registers[b];
if((registers[a] != 0) || ((parseInt(a) != 0) && (!isNaN(parseInt(a))))){
pc = pc + (parseInt(b) - 1);
}
}
instructionSet.tgl = (a)=>{
a = (Number.isInteger(parseInt(a))) ? parseInt(a) : registers[a];
if((pc+a) < instructions.length){
if(instructions[pc+a][1].length == 2){
instructions[pc+a][0] = (instructions[pc+a][0] == "jnz") ? "cpy" : "jnz";
}
else{
instructions[pc+a][0] = (instructions[pc+a][0] == "inc") ? "dec" : "inc";
}
}
}
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("jnz") > -1){
var instructionParts = instruction.match("jnz ([^ ]+) ([^ ]+)");
return ["jnz", [instructionParts[1], instructionParts[2]]];
}
if(instruction.indexOf("tgl") > -1){
var instructionParts = instruction.match("tgl ([^ ]+)");
return ["tgl", [instructionParts[1]]];
}
});
var pc = 0;
while(pc < instructions.length){
instructionSet[instructions[pc][0]].apply(null, instructions[pc][1]);
pc++;
}
console.log(registers);

77
day23/part2.js Normal file
View File

@ -0,0 +1,77 @@
var instructions = [];
var registers = {a: 12, b: 0, c: 0, d: 0};
var instructionSet = {};
instructionSet.cpy = (a, b)=>{
if(b in registers){
if(Number.isInteger(parseInt(a))){
registers[b] = parseInt(a);
}
else{
registers[b] = registers[a];
}
}
}
instructionSet.inc = (a)=>{
if(a in registers){
registers[a]++;
}
}
instructionSet.dec = (a)=>{
if(a in registers){
registers[a]--;
}
}
instructionSet.jnz = (a, b)=>{
b = (Number.isInteger(parseInt(b))) ? parseInt(b) : registers[b];
if((registers[a] != 0) || ((parseInt(a) != 0) && (!isNaN(parseInt(a))))){
pc = pc + (parseInt(b) - 1);
}
}
instructionSet.tgl = (a)=>{
a = (Number.isInteger(parseInt(a))) ? parseInt(a) : registers[a];
if((pc+a) < instructions.length){
if(instructions[pc+a][1].length == 2){
instructions[pc+a][0] = (instructions[pc+a][0] == "jnz") ? "cpy" : "jnz";
}
else{
instructions[pc+a][0] = (instructions[pc+a][0] == "inc") ? "dec" : "inc";
}
}
}
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("jnz") > -1){
var instructionParts = instruction.match("jnz ([^ ]+) ([^ ]+)");
return ["jnz", [instructionParts[1], instructionParts[2]]];
}
if(instruction.indexOf("tgl") > -1){
var instructionParts = instruction.match("tgl ([^ ]+)");
return ["tgl", [instructionParts[1]]];
}
});
var pc = 0;
while(pc < instructions.length){
instructionSet[instructions[pc][0]].apply(null, instructions[pc][1]);
pc++;
}
console.log(registers);