From fb78659f05d5d72c819d316bebc960fc48472bbd Mon Sep 17 00:00:00 2001 From: mhsn Date: Sat, 10 Jan 2026 12:31:57 +0000 Subject: 25-05 rust --- 2025/05/rust/Cargo.lock | 7 ++++++ 2025/05/rust/Cargo.toml | 6 ++++++ 2025/05/rust/src/main.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 2025/05/rust/Cargo.lock create mode 100644 2025/05/rust/Cargo.toml create mode 100644 2025/05/rust/src/main.rs (limited to '2025/05') diff --git a/2025/05/rust/Cargo.lock b/2025/05/rust/Cargo.lock new file mode 100644 index 0000000..0d1bf4e --- /dev/null +++ b/2025/05/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aoc_2025-05" +version = "0.1.0" diff --git a/2025/05/rust/Cargo.toml b/2025/05/rust/Cargo.toml new file mode 100644 index 0000000..e85826e --- /dev/null +++ b/2025/05/rust/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aoc_2025-05" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/2025/05/rust/src/main.rs b/2025/05/rust/src/main.rs new file mode 100644 index 0000000..cd78338 --- /dev/null +++ b/2025/05/rust/src/main.rs @@ -0,0 +1,55 @@ +use std::io; + +fn main() { + let mut input = io::stdin().lines().flatten().map(|line| line); + let ranges = input.by_ref().take_while(|s| !s.is_empty()).map(|s| { + let (lo, hi) = s.split_once('-').unwrap(); + ( + lo.to_owned().parse().unwrap(), + hi.to_owned().parse().unwrap(), + ) + }); + + let mut merged: Vec<(u64, u64)> = vec![]; + for (mut lo, mut hi) in ranges { + let ldx = merged.partition_point(|&(_, max)| max < lo); + let mut rdx = merged.partition_point(|&(_, max)| max < hi); + + let left = merged.get(ldx); + let right = merged.get(rdx); + + // ldx = merged.len() -> left is none -> insert at end + let Some(&(llo, lhi)) = left else { + merged.push((lo, hi)); + continue; + }; + + if (llo..=lhi).contains(&lo) { + lo = lo.min(llo); // include range start point + } + + // rdx = merged.len() -> right is none -> merge with all + let Some(&(rlo, rhi)) = right else { + merged.drain(ldx..); + merged.push((lo, hi)); + continue; + }; + + if (rlo..=rhi).contains(&hi) { + hi = hi.max(rhi); + rdx += 1; + } + + merged.drain(ldx..rdx); + merged.insert(ldx, (lo, hi)); + } + + let silver = input + .map(|n| n.parse::().unwrap()) + .filter(|id| merged.iter().any(|&(lo, hi)| (lo..=hi).contains(id))) + .count(); + let gold: u64 = merged.iter().map(|(lo, hi)| hi - lo + 1).sum(); + + println!("silver: {silver}"); + println!("gold: {gold}"); +} -- cgit v1.2.3