Added day 25

This commit is contained in:
Tim Stallard 2016-12-27 12:15:01 +00:00
parent 2a1b71d52f
commit 54c5bce75a
5 changed files with 163 additions and 0 deletions

30
day25/input.txt Normal file
View File

@ -0,0 +1,30 @@
cpy a d
cpy 7 c
cpy 362 b
inc d
dec b
jnz b -2
dec c
jnz c -5
cpy d a
jnz 0 0
cpy a b
cpy 0 a
cpy 2 c
jnz b 2
jnz 1 6
dec b
dec c
jnz c -4
inc a
jnz 1 -7
cpy 2 b
jnz c 2
jnz 1 4
dec b
dec c
jnz 1 -4
jnz 0 0
out b
jnz a -19
jnz 1 -21

73
day25/part1.js Normal file
View File

@ -0,0 +1,73 @@
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));

12
day25/test.js Normal file
View File

@ -0,0 +1,12 @@
var b = 8;
var a = 0;
var c = 2;
while(b != 0){
b = b - 1;
c = c - 1;
if(c == 0){
a++;
c = 2;
}
}
console.log(c);

36
day25/working_1.txt Normal file
View File

@ -0,0 +1,36 @@
d = a
c = 7
b = 362
d = d + (b*c)
b = 0
c = 0
IF 0
a = d
IF NOT 0
b = a
a = 0
c = 2
while b != 0
b = b - 1
c = c - 1
if c == 0
a = a + 1#
c = 2
end if
end while
if a odd c = 1
if a even c = 2
a = roundDown(a / 2)
b = 2
/*while c != 0
b = b - 1
c = c - 1
end if*/
b = b - c
c = 0
output b
GO BACK UP

12
day25/working_2.txt Normal file
View File

@ -0,0 +1,12 @@
d = a + (7*362)
b = 0
c = 0
(GOTO IF 0)
a = d
(GOTO IF NOT 0)
if a odd output 1
if a even output 0
a = a / 2
GOTO with a