From 3c4e0e431713f3e10cf2bc57f8100c782fa71c1d Mon Sep 17 00:00:00 2001 From: SebastianStork Date: Mon, 1 Dec 2025 19:08:37 +0100 Subject: [PATCH] Add unit tests to 2025 day 1 --- 2025/rust/day-01/src/main.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/2025/rust/day-01/src/main.rs b/2025/rust/day-01/src/main.rs index d8a8346..434c9ba 100644 --- a/2025/rust/day-01/src/main.rs +++ b/2025/rust/day-01/src/main.rs @@ -4,15 +4,14 @@ const STARTING_POSITION: i32 = 50; const DIAL_RANGE: i32 = 100; fn main() { - let rotations = read_rotations(); + let rotations = parse_rotations(&fs::read_to_string("../../inputs/01.txt").unwrap()); println!("Password1: {}", count_zero_positions(&rotations)); println!("Password2: {}", count_zero_clicks(&rotations)); } -fn read_rotations() -> Vec { - fs::read_to_string("../../inputs/01.txt") - .unwrap() +fn parse_rotations(contents: &str) -> Vec { + contents .lines() .map(|line| { let distance = line[1..].parse::().unwrap(); @@ -60,3 +59,29 @@ fn count_zero_clicks(rotations: &[i32]) -> i32 { counter } + +#[cfg(test)] +mod tests { + use super::*; + + const TEST_INPUT: &str = "L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82"; + + #[test] + fn test_part1() { + assert_eq!(count_zero_positions(&parse_rotations(TEST_INPUT)), 3); + } + + #[test] + fn test_part2() { + assert_eq!(count_zero_clicks(&parse_rotations(TEST_INPUT)), 6); + } +}