adventofcode-2016/day09/part2.js
2016-12-10 22:19:35 +00:00

29 lines
740 B
JavaScript

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));