From 00f434a9c1359ab1390b2c22a718ca8df413bbce Mon Sep 17 00:00:00 2001 From: Martijn Gerritsen Date: Fri, 5 Dec 2025 09:36:52 +0100 Subject: [PATCH] project structure update --- Cargo.toml | 4 ++ benches/bench_aoc_5.rs | 20 +++++++++ src/aoc.rs | 1 + src/aoc/aoc_5.rs | 72 +++++++++++++++++++++++++++++++ src/aoc/input/test_input_aoc5.txt | 11 +++++ src/main.rs | 16 +++++-- 6 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 benches/bench_aoc_5.rs create mode 100644 src/aoc/aoc_5.rs create mode 100644 src/aoc/input/test_input_aoc5.txt diff --git a/Cargo.toml b/Cargo.toml index c611915..4f45be0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,10 @@ harness = false [[bench]] name = "bench_aoc_4" harness = false +[[bench]] +name = "bench_aoc_5" +harness = false + [dependencies] [profile.bench] opt-level = 3 diff --git a/benches/bench_aoc_5.rs b/benches/bench_aoc_5.rs new file mode 100644 index 0000000..d40b237 --- /dev/null +++ b/benches/bench_aoc_5.rs @@ -0,0 +1,20 @@ +use std::hint::black_box; +use criterion::{criterion_group, criterion_main, Criterion}; +use aoc2025::aoc::aoc_5; + +fn bench_aoc_5_part1(c: &mut Criterion) { + let input = black_box(include_str!("../src/aoc/input/full_input_aoc5.txt")); + c.bench_function("bench_aoc5 part 1", |b| b.iter(|| aoc_5::solve_p1(input))); +} + +fn bench_aoc_5_part2(c: &mut Criterion) { + let input = black_box(include_str!("../src/aoc/input/full_input_aoc5.txt")); + c.bench_function("bench_aoc5 part 2", |b| b.iter(|| aoc_5::solve_p2(input))); +} + +criterion_group!( + benches_p5, + bench_aoc_5_part1, + bench_aoc_5_part2 +); +criterion_main!(benches_p5); \ No newline at end of file diff --git a/src/aoc.rs b/src/aoc.rs index bce5dfe..a11d9e7 100644 --- a/src/aoc.rs +++ b/src/aoc.rs @@ -2,3 +2,4 @@ pub mod aoc_1; pub mod aoc_2; pub mod aoc_3; pub mod aoc_4; +pub mod aoc_5; diff --git a/src/aoc/aoc_5.rs b/src/aoc/aoc_5.rs new file mode 100644 index 0000000..08456bc --- /dev/null +++ b/src/aoc/aoc_5.rs @@ -0,0 +1,72 @@ +pub fn solve_p1(input: &str) -> u64 { + let mut answer = 0; + let mut parse_range = true; + let mut ranges = Vec::new(); + let mut numbers = Vec::new(); + for line in input.lines() { + if line.is_empty() { + parse_range = false; + continue; + } + if parse_range { + let nums = line.split("-").map(|x| x.parse::().unwrap()).collect::>(); + ranges.push(nums); + } else { + let num = line.parse::().unwrap(); + numbers.push(num); + } + + } + ranges.sort(); + + for num in numbers { + for range in &ranges{ + if num >= range[0] { + if num <= range[1] { + answer += 1; + break; + } + } else { + break; + } + } + } + //println!("{:?} {:?}", ranges, numbers); + + + answer +} +pub fn solve_p2(input: &str) -> u64 { + let mut answer = 0; + let mut ranges = Vec::new(); + for line in input.lines() { + if line.is_empty() { + break; + } + let nums = line.split("-").map(|x| x.parse::().unwrap()).collect::>(); + ranges.push(nums); + } + ranges.sort(); + let mut new_ranges:Vec> = Vec::new(); + let mut new_range = Vec::::new(); + new_range.push(ranges[0][0]); + new_range.push(ranges[0][1]); + for range in ranges.iter() { + if range[0] >= new_range[0] && range[0] <= new_range[1] { + if range[1] > new_range[1] { + new_range[1] = range[1]; + } + }else if range[0] > new_range[1] { + new_ranges.push(Vec::from([new_range[0], new_range[1]])); + new_range[0] = range[0]; + new_range[1] = range[1]; + } + } + new_ranges.push(Vec::from([new_range[0], new_range[1]])); + for range in new_ranges { + answer += range[1] - range[0] + 1; + } + + + answer +} \ No newline at end of file diff --git a/src/aoc/input/test_input_aoc5.txt b/src/aoc/input/test_input_aoc5.txt new file mode 100644 index 0000000..ca4fb6b --- /dev/null +++ b/src/aoc/input/test_input_aoc5.txt @@ -0,0 +1,11 @@ +3-5 +10-14 +16-20 +12-18 + +1 +5 +8 +11 +17 +32 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b74c44d..e45d630 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,13 +11,21 @@ fn main() { let answer = aoc_3::solve_p1_fast(input2); println!("{}", answer); */ - + /* let input = include_bytes!("aoc/input/full_input_aoc4.txt"); - let answer = aoc_4::solve_p2_f(input); + let answer = aoc_4::solve_p1_f(input); println!("{}", answer); - let input = include_str!("aoc/input/full_input_aoc4.txt"); - let answer = aoc_4::solve_p2(input); + let input = include_bytes!("aoc/input/full_input_aoc4.txt"); + let answer = aoc_4::solve_p1(input); + println!("{}", answer); + */ + let input = include_str!("aoc/input/full_input_aoc5.txt"); + let answer = aoc_5::solve_p1(input); + println!("{}", answer); + + let input = include_str!("aoc/input/full_input_aoc5.txt"); + let answer = aoc_5::solve_p2(input); println!("{}", answer); }