Guest User

Untitled

a guest
Dec 4th, 2025
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const input = await Bun.file("input.txt").text();
  2.  
  3. const [rawRanges, rawIds] = input.split("\n\n");
  4. const ranges = rawRanges!.split("\n").map((r) => r.split("-").map((s) => +s) as [number, number]);
  5. const ids = rawIds!.split("\n").map((s) => +s);
  6.  
  7. let total = 0;
  8. for (const id of ids) {
  9.     if (ranges.some((r) => r[0] <= id && id <= r[1])) total++;
  10. }
  11.  
  12. let total2 = 0;
  13. let foundRanges: [number, number][] = [];
  14. for (const range of ranges) {
  15.     let size = range[1] - range[0] + 1;
  16.     const overlaps = foundRanges.filter((r) => range[0] <= r[1] && r[0] <= range[1]);
  17.     for (const overlap of overlaps) {
  18.         const overlapSize = Math.min(range[1], overlap[1]) - Math.max(range[0], overlap[0]) + 1;
  19.         size -= overlapSize;
  20.         if (range[0] <= overlap[0]) {
  21.             if (overlap[1] <= range[1]) {
  22.                 foundRanges.splice(foundRanges.indexOf(overlap), 1);
  23.             } else {
  24.                 overlap[0] = range[1] + 1;
  25.             }
  26.         } else {
  27.             overlap[1] = range[0] - 1;
  28.         }
  29.     }
  30.     total2 += size;
  31.     foundRanges.push(range);
  32. }
  33.  
  34. console.log("A:", total);
  35. console.log("B:", total2);
Advertisement
Add Comment
Please, Sign In to add comment