adventofcode-2016/day20/part2.js
2016-12-20 10:49:06 +00:00

24 lines
727 B
JavaScript

var input = require("fs").readFileSync("input.txt").toString().replace(/\r/g, "");
var ranges = input.split("\n").filter((a)=>(a)).map((line)=>(line.split("-").map((num)=>(parseInt(num))))).sort((a, b)=>(a[0] - b[0]));
var newRanges = ranges.reduce((fixed, range)=>{
if((fixed[fixed.length - 1][1] + 1) >= range[0]){
if((fixed[fixed.length - 1][1] + 1) < range[1]){
fixed[fixed.length - 1][1] = range[1];
}
}
else{
fixed.push(range);
}
return fixed;
}, [[0, 0]]);
var allowed = newRanges.reduce((numallowed, range, i, ranges)=>{
if(i < (ranges.length - 1)){
numallowed += ((ranges[i+1][0] - range[1]) - 1);
}
else{
numallowed += (4294967295 - range[1]);
}
return numallowed;
}, 0);
console.log(allowed);