diff --git a/day12/input.txt b/day12/input.txt new file mode 100644 index 0000000..3729d83 --- /dev/null +++ b/day12/input.txt @@ -0,0 +1,23 @@ +cpy 1 a +cpy 1 b +cpy 26 d +jnz c 2 +jnz 1 5 +cpy 7 c +inc d +dec c +jnz c -2 +cpy a c +inc a +dec b +jnz b -2 +cpy c b +dec d +jnz d -6 +cpy 19 c +cpy 11 d +inc a +dec d +jnz d -2 +dec c +jnz c -5 diff --git a/day12/part1.js b/day12/part1.js new file mode 100644 index 0000000..05a019f --- /dev/null +++ b/day12/part1.js @@ -0,0 +1,55 @@ +var instructions = []; +var registers = {a: 0, b: 0, c: 0, d: 0}; + +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) || ((parseInt(a) != 0) && (!isNaN(parseInt(a))))){ + pc = pc + (parseInt(b) - 1); + } +} + +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]]]; + } +}); + +var pc = 0; +while(pc < instructions.length){ + instructionSet[instructions[pc][0]].apply(null, instructions[pc][1]); + pc++; +} +console.log(registers); +console.log(registers[a]); diff --git a/day12/part2.js b/day12/part2.js new file mode 100644 index 0000000..365632f --- /dev/null +++ b/day12/part2.js @@ -0,0 +1,55 @@ +var instructions = []; +var registers = {a: 0, b: 0, c: 1, d: 0}; + +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) || ((parseInt(a) != 0) && (!isNaN(parseInt(a))))){ + pc = pc + (parseInt(b) - 1); + } +} + +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]]]; + } +}); + +var pc = 0; +while(pc < instructions.length){ + instructionSet[instructions[pc][0]].apply(null, instructions[pc][1]); + pc++; +} +console.log(registers); +console.log(registers[a]);