2025: day05
This commit is contained in:
parent
7433687d69
commit
82f19b2667
6 changed files with 1311 additions and 1 deletions
|
|
@ -4,12 +4,14 @@ use aoc2025::day01;
|
|||
use aoc2025::day02;
|
||||
use aoc2025::day03;
|
||||
use aoc2025::day04;
|
||||
use aoc2025::day05;
|
||||
|
||||
fn aoc2025_all(c: &mut Criterion) {
|
||||
c.bench_function("day01", |b| b.iter(|| day01::run().unwrap()));
|
||||
c.bench_function("day02", |b| b.iter(|| day02::run().unwrap()));
|
||||
c.bench_function("day03", |b| b.iter(|| day03::run().unwrap()));
|
||||
c.bench_function("day04", |b| b.iter(|| day04::run().unwrap()));
|
||||
c.bench_function("day05", |b| b.iter(|| day05::run().unwrap()));
|
||||
}
|
||||
|
||||
criterion_group! {
|
||||
|
|
|
|||
1168
aoc2025/input/day05.txt
Normal file
1168
aoc2025/input/day05.txt
Normal file
File diff suppressed because it is too large
Load diff
11
aoc2025/input/day05_provided.txt
Normal file
11
aoc2025/input/day05_provided.txt
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
3-5
|
||||
10-14
|
||||
16-20
|
||||
12-18
|
||||
|
||||
1
|
||||
5
|
||||
8
|
||||
11
|
||||
17
|
||||
32
|
||||
127
aoc2025/src/day05.rs
Normal file
127
aoc2025/src/day05.rs
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
use std::{
|
||||
fmt::Write,
|
||||
ops::{Bound, RangeBounds},
|
||||
};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
const INPUT: &str = include_str!("../input/day05.txt");
|
||||
|
||||
pub fn run() -> Result<String> {
|
||||
let mut res = String::with_capacity(128);
|
||||
writeln!(res, "part 1: {}", part1(INPUT)?)?;
|
||||
writeln!(res, "part 2: {}", part2(INPUT)?)?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> Result<u64> {
|
||||
let mut fresh_id_ranges = Vec::new();
|
||||
let mut available_ids = Vec::new();
|
||||
|
||||
let mut lines = input.lines();
|
||||
for line in lines.by_ref() {
|
||||
if line.is_empty() {
|
||||
break;
|
||||
}
|
||||
let (left, right) = line
|
||||
.split_once('-')
|
||||
.with_context(|| format!("failed to split range `{}' on `-'", line))?;
|
||||
let (left, right) = (left.parse::<u64>()?, right.parse::<u64>()?);
|
||||
let range = left..=right;
|
||||
fresh_id_ranges.push(range);
|
||||
}
|
||||
|
||||
for line in lines {
|
||||
available_ids.push(line.parse::<u64>()?);
|
||||
}
|
||||
|
||||
let mut count = 0;
|
||||
for id in available_ids {
|
||||
for range in &fresh_id_ranges {
|
||||
if range.contains(&id) {
|
||||
count += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
fn part2(input: &str) -> Result<usize> {
|
||||
let mut fresh_id_ranges = Vec::new();
|
||||
|
||||
for line in input.lines() {
|
||||
if line.is_empty() {
|
||||
break;
|
||||
}
|
||||
let (left, right) = line
|
||||
.split_once('-')
|
||||
.with_context(|| format!("failed to split range `{}' on `-'", line))?;
|
||||
let (left, right) = (left.parse::<u64>()?, right.parse::<u64>()?);
|
||||
let range = left..=right;
|
||||
fresh_id_ranges.push(range);
|
||||
}
|
||||
|
||||
// sort all ranges to prepare for merging of overlapping ranges
|
||||
let mut new_ranges = Vec::new();
|
||||
fresh_id_ranges.sort_by_key(|range| match (range.start_bound(), range.end_bound()) {
|
||||
(Bound::Included(&beg), Bound::Included(&end)) => (beg, end),
|
||||
_ => unreachable!("We're only handling inclusive ranges"),
|
||||
});
|
||||
|
||||
let mut fresh_id_ranges = fresh_id_ranges.into_iter();
|
||||
new_ranges.push(
|
||||
fresh_id_ranges
|
||||
.next()
|
||||
.context("Input did not contain any ranges!")?,
|
||||
);
|
||||
for range in fresh_id_ranges {
|
||||
let latest = new_ranges
|
||||
.last_mut()
|
||||
.expect("new_ranges always contains at least one element");
|
||||
let (last_start, last_end) = match (latest.start_bound(), latest.end_bound()) {
|
||||
(Bound::Included(&beg), Bound::Included(&end)) => (beg, end),
|
||||
_ => unreachable!("We're only handling inclusive ranges"),
|
||||
};
|
||||
let (cur_start, cur_end) = match (range.start_bound(), range.end_bound()) {
|
||||
(Bound::Included(&beg), Bound::Included(&end)) => (beg, end),
|
||||
_ => unreachable!("We're only handling inclusive ranges"),
|
||||
};
|
||||
|
||||
if cur_start <= last_end + 1 {
|
||||
*latest = (last_start)..=(last_end.max(cur_end));
|
||||
} else {
|
||||
new_ranges.push(range);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(new_ranges.into_iter().map(|r| r.count()).sum())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const PROVIDED: &str = include_str!("../input/day05_provided.txt");
|
||||
|
||||
#[test]
|
||||
fn part1_provided() {
|
||||
assert_eq!(part1(PROVIDED).unwrap(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1_real() {
|
||||
assert_eq!(part1(INPUT).unwrap(), 509);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_provided() {
|
||||
assert_eq!(part2(PROVIDED).unwrap(), 14);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_real() {
|
||||
assert_eq!(part2(INPUT).unwrap(), 336790092076620);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,3 +2,4 @@ pub mod day01;
|
|||
pub mod day02;
|
||||
pub mod day03;
|
||||
pub mod day04;
|
||||
pub mod day05;
|
||||
|
|
|
|||
|
|
@ -6,9 +6,10 @@ use aoc2025::day01;
|
|||
use aoc2025::day02;
|
||||
use aoc2025::day03;
|
||||
use aoc2025::day04;
|
||||
use aoc2025::day05;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let days: &[DayFunc] = &[day01::run, day02::run, day03::run, day04::run];
|
||||
let days: &[DayFunc] = &[day01::run, day02::run, day03::run, day04::run, day05::run];
|
||||
|
||||
aoc::run(days)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue