const input = await Bun.file("input.txt").text(); const [rawRanges, rawIds] = input.split("\n\n"); const ranges = rawRanges!.split("\n").map((r) => r.split("-").map((s) => +s) as [number, number]); const ids = rawIds!.split("\n").map((s) => +s); let total = 0; for (const id of ids) { if (ranges.some((r) => r[0] <= id && id <= r[1])) total++; } let total2 = 0; let foundRanges: [number, number][] = []; for (const range of ranges) { let size = range[1] - range[0] + 1; const overlaps = foundRanges.filter((r) => range[0] <= r[1] && r[0] <= range[1]); for (const overlap of overlaps) { const overlapSize = Math.min(range[1], overlap[1]) - Math.max(range[0], overlap[0]) + 1; size -= overlapSize; if (range[0] <= overlap[0]) { if (overlap[1] <= range[1]) { foundRanges.splice(foundRanges.indexOf(overlap), 1); } else { overlap[0] = range[1] + 1; } } else { overlap[1] = range[0] - 1; } } total2 += size; foundRanges.push(range); } console.log("A:", total); console.log("B:", total2);