From 2dac973d45c69c2c616b59354c2b6353ea2db245 Mon Sep 17 00:00:00 2001 From: Martijn Gerritsen Date: Mon, 1 Dec 2025 22:35:07 +0100 Subject: [PATCH] aoc_1 --- .gitignore | 1 + .idea/.gitignore | 8 ++ benches/bench_aoc_1.rs | 20 ++++ src/aoc.rs | 1 + src/aoc/aoc_1.rs | 159 ++++++++++++++++++++++++++++++ src/aoc/input/test_input_aoc1.txt | 10 ++ src/lib.rs | 1 + 7 files changed, 200 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 benches/bench_aoc_1.rs create mode 100644 src/aoc.rs create mode 100644 src/aoc/aoc_1.rs create mode 100644 src/aoc/input/test_input_aoc1.txt create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/benches/bench_aoc_1.rs b/benches/bench_aoc_1.rs new file mode 100644 index 0000000..53b463d --- /dev/null +++ b/benches/bench_aoc_1.rs @@ -0,0 +1,20 @@ +use aoc2025::aoc::aoc_1; +use criterion::{Criterion, criterion_group, criterion_main}; +use std::hint::black_box; + +fn bench_aoc_1_part1(c: &mut Criterion) { + let input = black_box(include_bytes!("../src/aoc/input/full_input_aoc1.txt")); + c.bench_function("bench_aoc1 part", |b| b.iter(|| aoc_1::solve_1(input))); +} + +fn bench_aoc_1_part2(c: &mut Criterion) { + let input = black_box(include_bytes!("../src/aoc/input/full_input_aoc1.txt")); + c.bench_function("bench_aoc1 part2", |b| b.iter(|| aoc_1::solve_2(input))); +} + +fn bench_aoc_1_part2_f(c: &mut Criterion) { + let input = black_box(include_bytes!("../src/aoc/input/full_input_aoc1.txt")); + c.bench_function("bench_aoc2 part fast", |b| b.iter(|| aoc_1::solve_2f(input))); +} +criterion_group!(benches_p1, bench_aoc_1_part1, bench_aoc_1_part2, bench_aoc_1_part2_f); +criterion_main!(benches_p1); diff --git a/src/aoc.rs b/src/aoc.rs new file mode 100644 index 0000000..6343537 --- /dev/null +++ b/src/aoc.rs @@ -0,0 +1 @@ +pub mod aoc_1; diff --git a/src/aoc/aoc_1.rs b/src/aoc/aoc_1.rs new file mode 100644 index 0000000..28c3dc7 --- /dev/null +++ b/src/aoc/aoc_1.rs @@ -0,0 +1,159 @@ +use std::hint::unreachable_unchecked; + +pub fn solve_1(input :&'static[u8]) -> u64 { + let mut dial = 50; + let mut answer =0; + + for line in input.split(|&b| b == b'\n') { + let len = line.len(); + + let val = if len == 2 { + // Vorm "L5" + (line[1] & 0x0F) as i64 + } else if len == 3 { + // Vorm "L50" + let tens = (line[1] & 0x0F) as i64; + let ones = (line[2] & 0x0F) as i64; + tens * 10 + ones + } else { + + let tens = (line[2] & 0x0F) as i64; + let ones = (line[3] & 0x0F) as i64; + tens * 10 + ones + }; + + if line[0] == b'R' { + dial += val; + }else { + dial += 100 - val; + } + if dial >= 100 { + dial -= 100; + } + if dial == 0 { answer += 1 } + } + answer +} + +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(); + + let val =match len { + 2 => line[1] as i64 - 48, + 3 => 10 * line[1] as i64 + line[2] as i64 - 48 - 480, + _ => {answer+=line[1] as u32 - 48; 10 * line[2] as i64 + line[ 3] as i64 - 48 - 480}, + }; + if input[0] == b'R' { + dial += val; + }else { + dial += 100 - val; + } + if dial >= 100 { + dial -= 100; + } + if dial == 0 { answer += 1 } + } + answer +} + +pub fn solve_2(input :&'static[u8]) -> u64 { + let mut dial = 50; + let mut answer =0; + + let mut index = 0; + let mut bytes = 0; + while index < input.len() { + if input[index] == b'\n' || index == input.len() - 1 { + if index == input.len() - 1 { + bytes +=1; + } + let num =match bytes { + 2 => input[index -1] as i64 - 48, + 3 => 10 * input[index -2] as i64 + input[index -1] as i64 - 48 - 480, + _ => {answer+=input[index-3] as u64 - 48; 10 * input[index -2] as i64 + input[index -1] as i64 - 48 - 480}, + }; + + match input[index - bytes] { + b'L' => { + + if num > dial { + if dial != 0 { + answer += 1; + } + dial = 100 - (num - dial); + } else { + dial = dial - num; + } + } + _ => { + if num + dial > 99 { + if num + dial != 100 { + answer += 1; + + dial = num + dial - 100; + + }else { + dial = 0; + } + + + } else { + dial = num + dial; + } + } + } + if dial == 0 { + answer += 1; + } + bytes = 0; + }else { + bytes +=1; + } + index +=1; + } + answer as u64 +} + +pub fn solve_2f(input :&'static[u8]) -> u64 { + let mut dial: i64 = 50; + let mut answer = 0; + + for line in input.split(|&b| b == b'\n') { + let len = line.len(); + + let val =match len { + 2 => line[1] as i64 - 48, + 3 => 10 * line[1] as i64 + line[2] as i64 - 48 - 480, + _ => {answer+=line[1] as u64 - 48; 10 * line[2] as i64 + line[ 3] as i64 - 48 - 480}, + }; + + if line[0] == b'L' { + let next_dial = dial - val; + if next_dial < 0 { + if dial != 0 { answer += 1; } + dial = next_dial + 100; + } else { + dial = next_dial; + } + } else { + // 'R' case + let next_dial = dial + val; + if next_dial >= 100 { + if next_dial != 100 { answer += 1; } + dial = next_dial - 100; + } else { + dial = next_dial; + } + } + + if dial == 0 { + answer += 1; + } + } + answer +} \ No newline at end of file diff --git a/src/aoc/input/test_input_aoc1.txt b/src/aoc/input/test_input_aoc1.txt new file mode 100644 index 0000000..d03fad7 --- /dev/null +++ b/src/aoc/input/test_input_aoc1.txt @@ -0,0 +1,10 @@ +L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..69f930e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod aoc;