As the forklifts break through the wall, the Elves are delighted to discover that there was a cafeteria on the other side after all.
You can hear a commotion coming from the kitchen. "At this rate, we won't have any time left to put the wreaths up in the dining hall!" Resolute in your quest, you investigate.
Visit the website for the full story and full puzzle description.
namespace AdventOfCode.Y2025.Day05;
using System;
using System.Collections.Generic;
using System.Linq;
record Range(long start, long end);
[ProblemName("Cafeteria")]
class Solution : Solver {
public object PartOne(string input) {
var (ranges, nums) = Parse(input);
return nums.Count(num => ranges.Any(range => range.start <= num && num <= range.end));
}
public object PartTwo(string input) {
// Merge overlapping ranges into disjoint intervals. First sort ranges
// by start so that any range that can extend the current one appears at
// a higher index. Then we walk the list once extending ranges[i] with any
// later range that overlaps with it.
var ranges = Parse(input).ranges.OrderBy(x => x.start).ToList();
for (var i = 0; i < ranges.Count; i++) {
int j = i + 1;
while (j < ranges.Count) {
if (ranges[j].start <= ranges[i].end) {
ranges[i] = new Range(ranges[i].start, Math.Max(ranges[i].end, ranges[j].end));
ranges.RemoveAt(j);
} else {
j++;
}
}
}
return ranges.Sum(range => range.end - range.start + 1);
}
(List<Range> ranges, long[] nums) Parse(string input) {
var blocks = input.Split("\n\n");
var ranges = (
from line in blocks[0].Split("\n")
let limits = line.Split("-").Select(long.Parse).ToArray()
select new Range(limits[0], limits[1])
).ToList();
var nums = blocks[1].Split("\n").Select(long.Parse).ToArray();
return (ranges, nums);
}
}Please ☆ my repo if you like it!