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

26 lines
669 B
JavaScript

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