From d803be0b017607f2fd83ea231b5a6e9fe8ff6d54 Mon Sep 17 00:00:00 2001 From: Russell Champoux Date: Thu, 4 Dec 2025 17:04:14 -0500 Subject: [PATCH] Solve Day 1: Secret Entrance --- README.md | 8 ++++ data/examples/01.txt | 11 +++++ src/bin/01.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 data/examples/01.txt create mode 100644 src/bin/01.rs diff --git a/README.md b/README.md index e3db903..70af4d3 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,14 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. + +## Benchmarks + +| Day | Part 1 | Part 2 | +| :---: | :---: | :---: | +| [Day 1](./src/bin/01.rs) | `113.0µs` | `119.9µs` | + +**Total: 0.23ms** --- diff --git a/data/examples/01.txt b/data/examples/01.txt new file mode 100644 index 0000000..5b04997 --- /dev/null +++ b/data/examples/01.txt @@ -0,0 +1,11 @@ +L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 + diff --git a/src/bin/01.rs b/src/bin/01.rs new file mode 100644 index 0000000..624c0ae --- /dev/null +++ b/src/bin/01.rs @@ -0,0 +1,98 @@ +advent_of_code::solution!(1); + +pub fn part_one(input: &str) -> Option { + let mut position: i32 = 50; + let mut count = 0u64; + + for line in input.lines() { + let line = line.trim(); + if line.is_empty() { + continue; + } + + let direction = &line[0..1]; + let distance: i32 = line[1..].parse().ok()?; + + match direction { + "L" => position = (position - distance).rem_euclid(100), + "R" => position = (position + distance).rem_euclid(100), + _ => return None, + } + + if position == 0 { + count += 1; + } + } + + Some(count) +} + +pub fn part_two(input: &str) -> Option { + let mut position: i32 = 50; + let mut count = 0u64; + + for line in input.lines() { + let line = line.trim(); + if line.is_empty() { + continue; + } + + let direction = &line[0..1]; + let distance: i32 = line[1..].parse().ok()?; + + // Count how many times we pass through 0 during this rotation + let zeros = match direction { + "L" => { + // Moving left: we hit 0 at clicks p, p+100, p+200, ... (if p > 0) + // or at clicks 100, 200, 300, ... (if p = 0) + if position == 0 { + distance / 100 + } else if distance >= position { + (distance - position) / 100 + 1 + } else { + 0 + } + } + "R" => { + // Moving right: we hit 0 at click (100-p), then (100-p)+100, etc. + // or at clicks 100, 200, 300, ... (if p = 0) + if position == 0 { + distance / 100 + } else if distance >= 100 - position { + (distance + position - 100) / 100 + 1 + } else { + 0 + } + } + _ => return None, + }; + + count += zeros as u64; + + // Update position + match direction { + "L" => position = (position - distance).rem_euclid(100), + "R" => position = (position + distance).rem_euclid(100), + _ => {} + } + } + + Some(count) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(3)); + } + + #[test] + fn test_part_two() { + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(6)); + } +}