Added day 9

This commit is contained in:
Tim Stallard 2016-12-10 22:19:35 +00:00
parent 6e3de19949
commit 9bfed12c61
3 changed files with 54 additions and 0 deletions

1
day09/input.txt Normal file

File diff suppressed because one or more lines are too long

25
day09/part1.js Normal file
View File

@ -0,0 +1,25 @@
var input = require("fs").readFileSync("input.txt").toString().replace(/\n/g, "").replace(/\r/g, "");
var parts = [];
while(input.length > 0){
if(input[0] == "("){
var len = input.match(/(\(([0-9]+)x([0-9]+)\)).*/);
parts.push(input.slice(0, len[1].length + parseInt(len[2])));
input = input.slice(len[1].length + parseInt(len[2]));
}
else{
parts.push(input[0]);
input = input.slice(1);
}
}
parts = parts.map(function(part){
if(part[0] == "("){
var len = part.match(/(\(([0-9]+)x([0-9]+)\))(.*)/);
return parseInt(len[2]) * parseInt(len[3]);
}
else{
return part.length
}
});
console.log(parts.reduce((a, b)=>(a + b)));

28
day09/part2.js Normal file
View File

@ -0,0 +1,28 @@
var input = require("fs").readFileSync("input.txt").toString().replace(/\n/g, "").replace(/\r/g, "");
function lenStr(str){
var parts = [];
while(str.length > 0){
if(str[0] == "("){
var len = str.match(/(\(([0-9]+)x([0-9]+)\)).*/);
parts.push(str.slice(0, len[1].length + parseInt(len[2])));
str = str.slice(len[1].length + parseInt(len[2]));
}
else{
parts.push(str[0]);
str = str.slice(1);
}
}
parts = parts.map(function(part){
if(part[0] == "("){
var len = part.match(/(\(([0-9]+)x([0-9]+)\))(.*)/);
return parseInt(len[3]) * lenStr(len[4]);
}
else{
return part.length;
}
});
return parts.reduce((a, b)=>(a + b));
}
console.log(lenStr(input));