Added day 12
This commit is contained in:
parent
e532fa96f6
commit
a7cb0c3066
23
day12/input.txt
Normal file
23
day12/input.txt
Normal file
@ -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
|
55
day12/part1.js
Normal file
55
day12/part1.js
Normal file
@ -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]);
|
55
day12/part2.js
Normal file
55
day12/part2.js
Normal file
@ -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]);
|
Loading…
x
Reference in New Issue
Block a user