this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Solve D05P02

modamo-gh c1021950 e9848fc9

+30
+30
day05/part2.ts
··· 1 + import { readFileSync } from "fs"; 2 + 3 + const [rawRanges, _] = readFileSync("./input.txt", "utf8").split(/\n\n/); 4 + 5 + let ranges = rawRanges 6 + .split(/\n/) 7 + .map((range) => { 8 + const [lowerBound, upperBound] = range.split("-").map(Number); 9 + 10 + return { lowerBound, upperBound }; 11 + }) 12 + .sort((a, b) => a.lowerBound - b.lowerBound); 13 + let i = 1; 14 + 15 + while (i < ranges.length) { 16 + if ( 17 + ranges[i].lowerBound >= ranges[i - 1].lowerBound && 18 + ranges[i].lowerBound <= ranges[i - 1].upperBound 19 + ) { 20 + ranges[i - 1].upperBound = Math.max( 21 + ranges[i - 1].upperBound, 22 + ranges[i].upperBound 23 + ); 24 + ranges = [...ranges.slice(0, i), ...ranges.slice(i + 1)]; 25 + } else { 26 + i++; 27 + } 28 + } 29 + 30 + console.log(ranges.reduce((a, c) => a + c.upperBound - c.lowerBound + 1, 0));