1
0

Add solution for "Day 1: Secret Entrance", part 2

This commit is contained in:
Stefan Müller
2026-01-27 22:36:49 +01:00
parent b5665199e1
commit af14e87459
4 changed files with 48 additions and 21 deletions
+8 -2
View File
@@ -1,6 +1,6 @@
# AdventOfCode2025 # AdventOfCode2025
Solver for Advent of Code 2025 puzzles. https://adventofcode.com/2025/ Solver for Advent of Code 2025 puzzles. <https://adventofcode.com/2025/>
This is a single command line application for the puzzles written in [Rust](https://rust-lang.org/). 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 ## Solutions
### Day 1: Secret Entrance
:mag_right: Puzzle: <https://adventofcode.com/2025/day/1>, :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 ### Day 4: Printing Department
:mag_right: Puzzle: <https://adventofcode.com/2025/day/4>, :white_check_mark: Solver: [`PrintingDepartment`](src/solvers/printing_department.rs) :mag_right: Puzzle: <https://adventofcode.com/2025/day/4>, :white_check_mark: Solver: [`PrintingDepartment`](src/solvers/printing_department.rs)
@@ -36,7 +42,7 @@ The package contains unit tests for each solver to help troubleshoot issues and
## License ## 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 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.
+37 -16
View File
@@ -6,9 +6,30 @@ pub struct SecretEntrance {}
impl SecretEntrance { impl SecretEntrance {
const START_POSITION: i32 = 50; const START_POSITION: i32 = 50;
const DIAL_SIZE: i32 = 100; const DIAL_SIZE: i32 = 100;
const COUNT_POSITION: i32 = 0; const ZERO_POSITION: i32 = 0;
const POSITIVE_SIGN_CHAR: char = 'R'; const POSITIVE_SIGN_CHAR: char = 'R';
const NEGATIVE_SIGN_CHAR: char = 'L'; 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 { impl Solver for SecretEntrance {
@@ -17,26 +38,26 @@ impl Solver for SecretEntrance {
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64) { fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64) {
let mut position = Self::START_POSITION; let mut position = Self::START_POSITION;
let mut count = 0; let mut zero_hits = 0;
let format = format!( let mut zero_passes = 0;
let format: String = format!(
"{{[{}{}]}}{{d}}", "{{[{}{}]}}{{d}}",
Self::POSITIVE_SIGN_CHAR, Self::POSITIVE_SIGN_CHAR,
Self::NEGATIVE_SIGN_CHAR Self::NEGATIVE_SIGN_CHAR
); );
for line in reader.lines().map_while(Result::ok) { for line in reader.lines().map_while(Result::ok) {
if let Ok((sign, distance)) = scan_fmt!(&line, &format, char, i32) { let (sign, distance) = scan_fmt!(&line, &format, char, i32).unwrap();
position = (position (position, zero_hits, zero_passes) = Self::update_position(
+ if sign == Self::POSITIVE_SIGN_CHAR { position,
distance if sign == Self::POSITIVE_SIGN_CHAR {
} else { distance
-distance } else {
}) -distance
.rem_euclid(Self::DIAL_SIZE); },
} zero_hits,
if position == Self::COUNT_POSITION { zero_passes,
count = count + 1; );
}
} }
(count, 0) (zero_hits, zero_passes)
} }
} }
+1 -1
View File
@@ -11,7 +11,7 @@ const EXAMPLE_PATHS: &'static [&'static str] = &[
#[test] #[test]
fn secret_entrance() { fn secret_entrance() {
assert_eq!( assert_eq!(
Ok((3, 0)), Ok((3, 6)),
solvers::run_solver(solvers::secret_entrance::SecretEntrance {}, &EXAMPLE_PATHS) solvers::run_solver(solvers::secret_entrance::SecretEntrance {}, &EXAMPLE_PATHS)
); );
} }
+1 -1
View File
@@ -3,7 +3,7 @@ use advent_of_code_2025::*;
#[test] #[test]
fn secret_entrance() { fn secret_entrance() {
assert_eq!( assert_eq!(
Ok((1086, 0)), Ok((1086, 6268)),
solvers::run_solver( solvers::run_solver(
solvers::secret_entrance::SecretEntrance {}, solvers::secret_entrance::SecretEntrance {},
&solvers::DATA_PATHS &solvers::DATA_PATHS