Day 20
This commit is contained in:
parent
a2bbd41f78
commit
93823203f3
1075
day20/input.txt
Normal file
1075
day20/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
5
day20/part1.js
Normal file
5
day20/part1.js
Normal file
@ -0,0 +1,5 @@
|
||||
var min = 0;
|
||||
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 min = ranges.reduce((min, range)=>((min >= range[0]) && (min <= range[1]) ? (range[1] + 1) : min), 0);
|
||||
console.log(min);
|
23
day20/part2.js
Normal file
23
day20/part2.js
Normal file
@ -0,0 +1,23 @@
|
||||
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);
|
Loading…
Reference in New Issue
Block a user