From 6c162bc7bcbe01ded2a6666b26538b198e4771e7 Mon Sep 17 00:00:00 2001 From: Tim Stallard Date: Sat, 24 Dec 2016 21:06:34 +0000 Subject: [PATCH] Day 23 --- day23/input.txt | 26 +++++++++++++++++ day23/part1.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ day23/part2.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 day23/input.txt create mode 100644 day23/part1.js create mode 100644 day23/part2.js diff --git a/day23/input.txt b/day23/input.txt new file mode 100644 index 0000000..336cd3e --- /dev/null +++ b/day23/input.txt @@ -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 diff --git a/day23/part1.js b/day23/part1.js new file mode 100644 index 0000000..71560e4 --- /dev/null +++ b/day23/part1.js @@ -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); diff --git a/day23/part2.js b/day23/part2.js new file mode 100644 index 0000000..7e049ff --- /dev/null +++ b/day23/part2.js @@ -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);