Public
Edited
Aug 27, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
function parse(input) {
return input
.split("\n")
.map((line) => line.split("-").map(Number))
.sort((a, b) => a[0] - b[0]);
}
Insert cell
Insert cell
function findLowestUnblockedIP(ranges) {
let ip = 0;
for (const [start, end] of ranges) {
if (ip < start) {
return ip;
}
ip = Math.max(ip, end + 1);
}
return ip;
}
Insert cell
function part1(input) {
return findLowestUnblockedIP(parse(input));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function countUnblockedIPs(ranges) {
const maxIP = 4294967295;
let [ip, unblocked] = [0, 0];
for (const [start, end] of ranges) {
if (ip < start) {
unblocked += start - ip;
}
ip = Math.max(ip, end + 1);
}
// Add any remaining IPs between the last upper bound and the maximim permitted IP
if (ip <= maxIP) {
unblocked += maxIP - ip + 1;
}
return unblocked;
}
Insert cell
function part2(input) {
return countUnblockedIPs(parse(input));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more