Last active
December 5, 2025 09:51
-
-
Save dkaraush/3a36d7191142dca0457aa4ae98b2e494 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| input = document.querySelector('pre').innerText; | |
| [ranges, ids] = input.split('\n\n').map(x => x.split('\n')); | |
| ranges = ranges.map(r => r.split('-').map(x => parseInt(x))); | |
| console.log(ids.filter(id => ranges.some(r => id >= r[0] && id <= r[1])).length) | |
| function mergeRanges() { | |
| const N = ranges.length | |
| for (let i = 0; i < N; ++i) | |
| for (let j = 0; j < N; ++j) { | |
| const [a, b] = [ranges[i], ranges[j]] | |
| if (i == j || !a || !b) continue | |
| if (b[0] <= a[1] && a[0] <= b[0]) { | |
| ranges[i] = [Math.min(a[0], b[0]), Math.max(a[1], b[1])] | |
| ranges[j] = undefined | |
| } | |
| } | |
| return N - (ranges = ranges.filter(r => !!r)).length | |
| } | |
| while (mergeRanges() > 0); | |
| console.log(ranges.reduce((a, [from, to]) => a + (to - from + 1), 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment