From baa97512d6ab01a471a873f0a225f1a254a6bc70 Mon Sep 17 00:00:00 2001 From: Martijn Gerritsen Date: Tue, 2 Dec 2025 14:54:42 +0100 Subject: [PATCH] aoc_2 --- Cargo.toml | 4 ++++ benches/bench_aoc_2.rs | 16 +++++++++++++ src/aoc.rs | 1 + src/aoc/aoc_1.rs | 3 +-- src/aoc/aoc_2.rs | 39 +++++++++++++++++++++++++++++++ src/aoc/input/test_input_aoc2.txt | 1 + src/main.rs | 21 ++++++++++++++--- 7 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 benches/bench_aoc_2.rs create mode 100644 src/aoc/aoc_2.rs create mode 100644 src/aoc/input/test_input_aoc2.txt diff --git a/Cargo.toml b/Cargo.toml index ae1360a..a296c6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,8 @@ criterion = { version = "0.7", features = ["html_reports"] } [[bench]] name = "bench_aoc_1" +harness = false + +[[bench]] +name = "bench_aoc_2" harness = false \ No newline at end of file diff --git a/benches/bench_aoc_2.rs b/benches/bench_aoc_2.rs new file mode 100644 index 0000000..e76af14 --- /dev/null +++ b/benches/bench_aoc_2.rs @@ -0,0 +1,16 @@ +use aoc2025::aoc::aoc_2; +use criterion::{Criterion, criterion_group, criterion_main}; +use std::hint::black_box; + +fn bench_aoc_2_part1(c: &mut Criterion) { + let input = black_box(include_str!("../src/aoc/input/full_input_aoc2.txt")); + c.bench_function("bench_aoc2 part 1", |b| b.iter(|| aoc_2::solve_p1(input))); +} + +fn bench_aoc_2_part2(c: &mut Criterion) { + let input = black_box(include_str!("../src/aoc/input/full_input_aoc2.txt")); + c.bench_function("bench_aoc2 part 1", |b| b.iter(|| aoc_2::solve_p2(input))); +} + +criterion_group!(benches_p2, bench_aoc_2_part1, bench_aoc_2_part2); +criterion_main!(benches_p2); \ No newline at end of file diff --git a/src/aoc.rs b/src/aoc.rs index 6343537..1a48657 100644 --- a/src/aoc.rs +++ b/src/aoc.rs @@ -1 +1,2 @@ pub mod aoc_1; +pub mod aoc_2; diff --git a/src/aoc/aoc_1.rs b/src/aoc/aoc_1.rs index 28c3dc7..dab6a4a 100644 --- a/src/aoc/aoc_1.rs +++ b/src/aoc/aoc_1.rs @@ -1,4 +1,3 @@ -use std::hint::unreachable_unchecked; pub fn solve_1(input :&'static[u8]) -> u64 { let mut dial = 50; @@ -39,7 +38,7 @@ pub fn solve_1_f(input :&'static[u8]) -> u32 { let mut dial = 50; let mut answer =0; - let mut index = 0; + for line in input.split(|&b| b == b'\n') { let len = line.len(); diff --git a/src/aoc/aoc_2.rs b/src/aoc/aoc_2.rs new file mode 100644 index 0000000..646101c --- /dev/null +++ b/src/aoc/aoc_2.rs @@ -0,0 +1,39 @@ +use std::io::BufRead; + +pub fn solve_p1(input: &str ) -> u64 { + let mut answer = 0; + for line in input.split(',') { + let (num1, num2) = line.split_once('-').map(|n| {(n.0.parse::().unwrap(), n.1.parse::().unwrap())}).unwrap(); + for i in num1..=num2 { + let text = i.to_string(); + if text.len()%2 == 0 { + let splits = text.split_at(text.len()/2); + if splits.0==splits.1 { + answer +=i; + } + } + } + } + + + answer +} + +pub fn solve_p2(input: &str ) -> u64 { + let mut answer = 0; + for line in input.split(',') { + let num_range = line.split('-').map(|num| { num.parse::().unwrap_or(0) }).collect::>(); + for i in num_range[0]..=num_range[1] { + let text = i.to_string(); + for j in 1..=text.len() / 2 { + if text.len() % j == 0 { + if text[0..j].repeat(text.len() / j) == text { + answer += i; + break; + } + } + } + } + } + answer +} \ No newline at end of file diff --git a/src/aoc/input/test_input_aoc2.txt b/src/aoc/input/test_input_aoc2.txt new file mode 100644 index 0000000..bd04584 --- /dev/null +++ b/src/aoc/input/test_input_aoc2.txt @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ff43b9e..5a28713 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,16 @@ + + pub mod aoc; use crate::aoc::*; fn main() { - println!("answer: {}", aoc_1::solve_1_f( include_bytes!("aoc/input/full_input_aoc1.txt"))); - println!("answer: {}", aoc_1::solve_2(include_bytes!("aoc/input/full_input_aoc1.txt"))); - //println!("{:?}", include_bytes!("aoc/input/full_input_aoc1.txt")); + let input = include_str!("aoc/input/full_input_aoc2.txt"); + let answer = aoc_2::solve_p1(input); + println!("{}", answer); + + let input = include_bytes!("aoc/input/test_input_aoc2.txt"); + //let answer = aoc_2::solve_p1_f(input); + println!("{:?}", input); } #[test] @@ -18,3 +24,12 @@ fn test_aoc_1_2() { assert_eq!(aoc_1::solve_2f(include_bytes!("aoc/input/test_input_aoc1.txt")), 6); assert_eq!(aoc_1::solve_2f(include_bytes!("aoc/input/full_input_aoc1.txt")), 6770); } + +#[test] +fn test_aoc_2_part1() { + assert_eq!(aoc_2::solve_p1(include_str!("aoc/input/test_input_aoc2.txt")), 1227775554); +} +#[test] +fn test_aoc_2_part2() { + assert_eq!(aoc_2::solve_p2(include_str!("aoc/input/test_input_aoc2.txt")), 4174379265); +} \ No newline at end of file