diff --git a/README.md b/README.md index c193e50..f036315 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # AdventOfCode2025 -Solver for Advent of Code 2025 puzzles. https://adventofcode.com/2025/ +Solver for Advent of Code 2025 puzzles. This is a single command line application for the puzzles written in [Rust](https://rust-lang.org/). @@ -10,6 +10,12 @@ This project does not contain the puzzle or example inputs as per the [copyright ## Solutions +### Day 1: Secret Entrance + +:mag_right: Puzzle: , :white_check_mark: Solver: [`SecretEntrance`](src/solvers/secret_entrance.rs) + +For this one, we are moving around a dial with some not too complicated modulo calculations. + ### Day 4: Printing Department :mag_right: Puzzle: , :white_check_mark: Solver: [`PrintingDepartment`](src/solvers/printing_department.rs) @@ -36,10 +42,10 @@ The package contains unit tests for each solver to help troubleshoot issues and ## License -Copyright (C) 2025 Stefan Müller +Copyright (C) 2025-2026 Stefan Müller This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -You should have received a copy of the GNU General Public License along with this program. If not, see . \ No newline at end of file +You should have received a copy of the GNU General Public License along with this program. If not, see . diff --git a/src/solvers/secret_entrance.rs b/src/solvers/secret_entrance.rs index 14ef7ad..f9f7c3b 100644 --- a/src/solvers/secret_entrance.rs +++ b/src/solvers/secret_entrance.rs @@ -6,9 +6,30 @@ pub struct SecretEntrance {} impl SecretEntrance { const START_POSITION: i32 = 50; const DIAL_SIZE: i32 = 100; - const COUNT_POSITION: i32 = 0; + const ZERO_POSITION: i32 = 0; const POSITIVE_SIGN_CHAR: char = 'R'; const NEGATIVE_SIGN_CHAR: char = 'L'; + + fn update_position( + position: i32, + distance: i32, + mut zero_hits: u64, + mut zero_passes: u64, + ) -> (i32, u64, u64) { + let mut new_position = position + distance; + let rounds = new_position.div_euclid(Self::DIAL_SIZE); + zero_passes += u64::try_from(rounds.abs()).unwrap(); + new_position -= Self::DIAL_SIZE * rounds; + if new_position == Self::ZERO_POSITION { + zero_hits += 1; + if position != Self::ZERO_POSITION && distance < 0 { + zero_passes += 1; + } + } else if position == Self::ZERO_POSITION && distance < 0 { + zero_passes -= 1; + } + (new_position, zero_hits, zero_passes) + } } impl Solver for SecretEntrance { @@ -17,26 +38,26 @@ impl Solver for SecretEntrance { fn process_data(&self, reader: R) -> (u64, u64) { let mut position = Self::START_POSITION; - let mut count = 0; - let format = format!( + let mut zero_hits = 0; + let mut zero_passes = 0; + let format: String = format!( "{{[{}{}]}}{{d}}", Self::POSITIVE_SIGN_CHAR, Self::NEGATIVE_SIGN_CHAR ); for line in reader.lines().map_while(Result::ok) { - if let Ok((sign, distance)) = scan_fmt!(&line, &format, char, i32) { - position = (position - + if sign == Self::POSITIVE_SIGN_CHAR { - distance - } else { - -distance - }) - .rem_euclid(Self::DIAL_SIZE); - } - if position == Self::COUNT_POSITION { - count = count + 1; - } + let (sign, distance) = scan_fmt!(&line, &format, char, i32).unwrap(); + (position, zero_hits, zero_passes) = Self::update_position( + position, + if sign == Self::POSITIVE_SIGN_CHAR { + distance + } else { + -distance + }, + zero_hits, + zero_passes, + ); } - (count, 0) + (zero_hits, zero_passes) } } diff --git a/tests/examples.rs b/tests/examples.rs index 983f370..77b337a 100644 --- a/tests/examples.rs +++ b/tests/examples.rs @@ -11,7 +11,7 @@ const EXAMPLE_PATHS: &'static [&'static str] = &[ #[test] fn secret_entrance() { assert_eq!( - Ok((3, 0)), + Ok((3, 6)), solvers::run_solver(solvers::secret_entrance::SecretEntrance {}, &EXAMPLE_PATHS) ); } diff --git a/tests/full_data.rs b/tests/full_data.rs index b005056..59081b6 100644 --- a/tests/full_data.rs +++ b/tests/full_data.rs @@ -3,7 +3,7 @@ use advent_of_code_2025::*; #[test] fn secret_entrance() { assert_eq!( - Ok((1086, 0)), + Ok((1086, 6268)), solvers::run_solver( solvers::secret_entrance::SecretEntrance {}, &solvers::DATA_PATHS