mirror of
https://github.com/RussellChamp/advent-of-code-2025-cursor.git
synced 2026-10-27 23:52:34 +00:00
Solve Day 1: Secret Entrance
This commit is contained in:
1 parent
cfeb0828fd
commit
d803be0b01
3 files changed
+117
No files matched your search
@@ -6,6 +6,14 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
|
||||
|
||||
<!--- advent_readme_stars table --->
|
||||
|
||||
<!--- benchmarking table --->
|
||||
## Benchmarks
|
||||
|
||||
| Day | Part 1 | Part 2 |
|
||||
| :---: | :---: | :---: |
|
||||
| [Day 1](./src/bin/01.rs) | `113.0µs` | `119.9µs` |
|
||||
|
||||
**Total: 0.23ms**
|
||||
<!--- benchmarking table --->
|
||||
|
||||
---
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
advent_of_code::solution!(1);
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u64> {
|
||||
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<u64> {
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user