From e2af8dddbe2260abbdd5406dce0a900af267b49d Mon Sep 17 00:00:00 2001 From: SebastianStork Date: Mon, 1 Dec 2025 15:43:14 +0100 Subject: [PATCH] Solve 2025 day 1 part 2 in rust --- 2025/rust/day-01/src/main.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/2025/rust/day-01/src/main.rs b/2025/rust/day-01/src/main.rs index 6f15cf3..ca04183 100644 --- a/2025/rust/day-01/src/main.rs +++ b/2025/rust/day-01/src/main.rs @@ -1,12 +1,13 @@ use std::fs::{self}; fn main() { - let rotations = read_input(); + let rotations = read_rotations(); - println!("Password: {}", count_neutral_positions(rotations)); + println!("Password1: {}", count_zero_positions(&rotations)); + println!("Password2: {}", count_zero_clicks(&rotations)); } -fn read_input() -> Vec { +fn read_rotations() -> Vec { let contents = fs::read_to_string("../../inputs/01.txt").unwrap(); let lines = contents.lines(); @@ -18,7 +19,7 @@ fn read_input() -> Vec { let distance = distance.parse::().unwrap(); let rotation = match direction { - "L" => distance * -1, + "L" => -distance, "R" => distance, _ => panic!(), }; @@ -29,12 +30,12 @@ fn read_input() -> Vec { rotations } -fn count_neutral_positions(rotations: Vec) -> i32 { +fn count_zero_positions(rotations: &Vec) -> i32 { let mut counter = 0; let mut position = 50; for rotation in rotations { - position = (position + rotation) % 100; + position = (position + rotation).rem_euclid(100); if position == 0 { counter += 1; @@ -43,3 +44,24 @@ fn count_neutral_positions(rotations: Vec) -> i32 { counter } + +fn count_zero_clicks(rotations: &Vec) -> i32 { + let mut counter = 0; + let mut position = 50; + + for rotation in rotations { + position += rotation; + + counter += (position / 100).abs(); + if position < 0 && position != *rotation { + counter += 1; + } + if position == 0 { + counter += 1; + } + + position = position.rem_euclid(100); + } + + counter +}