From 796138a9e48c25279670bbe3fc36ef3d08c986db Mon Sep 17 00:00:00 2001 From: Ben C Date: Fri, 5 Dec 2025 07:47:36 -0500 Subject: [PATCH] Day 5 --- utils/src/range.rs | 17 ++++++++- years/2025/src/day_5.rs | 56 +++++++++++++++++++++++++---- years/2025/src/examples/day_5/1.txt | 11 ++++++ years/2025/src/examples/day_5/2.txt | 11 ++++++ 4 files changed, 88 insertions(+), 7 deletions(-) diff --git a/utils/src/range.rs b/utils/src/range.rs index 0e842fe..de3f76f 100644 --- a/utils/src/range.rs +++ b/utils/src/range.rs @@ -51,7 +51,22 @@ impl BetterRange { /// ``` /// pub fn contains_range(&self, other: &Self) -> bool { - self.start <= other.start && other.end < self.end + self.start <= other.start && other.end <= self.end + } + + /// Merge this range with another. If the ranges do not intersect, [None] is returned. + pub fn merge(&self, other: &Self) -> Option { + if self.contains_range(other) { + Some(*self) + } else if other.contains_range(self) { + Some(*other) + } else if self.contains(&other.start) || self.end == other.start { + Some(Self::new(self.start, other.end)) + } else if self.contains(&other.end) || other.end == self.start { + Some(Self::new(other.start, self.end)) + } else { + None + } } /// Split the range at the given value. diff --git a/years/2025/src/day_5.rs b/years/2025/src/day_5.rs index a87ca56..98de929 100644 --- a/years/2025/src/day_5.rs +++ b/years/2025/src/day_5.rs @@ -1,17 +1,61 @@ - use advent_core::{Day, day_stuff, ex_for_day}; +use utils::range::BetterRange; pub struct Day5; impl Day for Day5 { + day_stuff!(5, "3", "14", (Vec>, Vec)); + + fn part_1((ranges, ids): Self::Input) -> Option { + let ans = ids + .into_iter() + .filter(|id| ranges.iter().any(|range| range.contains(id))) + .count(); + + Some(ans.to_string()) + } + + fn part_2((mut ranges, _): Self::Input) -> Option { + ranges.sort_by(|a, b| a.start.cmp(&b.start).then(a.end.cmp(&b.end))); - day_stuff!(5, "", ""); + let mut new_ranges = Vec::with_capacity(ranges.len()); - fn part_1(_input: Self::Input) -> Option { - None + for range in ranges { + let mut merged = false; + + for new_range in new_ranges.iter_mut() { + if let Some(merged_range) = range.merge(new_range) { + merged = true; + *new_range = merged_range; + break; + } + } + + if !merged { + new_ranges.push(range); + } + } + + let ans = new_ranges + .into_iter() + .map(|range| range.end - range.start) + .sum::(); + + Some(ans.to_string()) } - fn part_2(_input: Self::Input) -> Option { - None + fn parse_input(input: &str) -> Self::Input { + let (ranges, ids) = input.split_once("\n\n").unwrap(); + + ( + ranges + .lines() + .map(|l| { + let (lower, upper) = l.split_once('-').unwrap(); + BetterRange::new(lower.parse().unwrap(), upper.parse::().unwrap() + 1) + }) + .collect(), + ids.lines().map(|l| l.parse().unwrap()).collect(), + ) } } diff --git a/years/2025/src/examples/day_5/1.txt b/years/2025/src/examples/day_5/1.txt index e69de29..2e9078d 100644 --- a/years/2025/src/examples/day_5/1.txt +++ b/years/2025/src/examples/day_5/1.txt @@ -0,0 +1,11 @@ +3-5 +10-14 +16-20 +12-18 + +1 +5 +8 +11 +17 +32 diff --git a/years/2025/src/examples/day_5/2.txt b/years/2025/src/examples/day_5/2.txt index e69de29..2e9078d 100644 --- a/years/2025/src/examples/day_5/2.txt +++ b/years/2025/src/examples/day_5/2.txt @@ -0,0 +1,11 @@ +3-5 +10-14 +16-20 +12-18 + +1 +5 +8 +11 +17 +32 -- 2.51.2