Skip to content

Commit 22fcdc8

Browse files
committed
Day 01
1 parent 01e3348 commit 22fcdc8

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,25 @@ By default, `cargo time` does not write to the readme. In order to do so, append
189189
> [!NOTE]
190190
> Please note that these are not _scientific_ benchmarks, understand them as a fun approximation. 😉 Timings, especially
191191
> in the microseconds range, might change a bit between invocations.
192+
193+
## Results
194+
195+
<!--- advent_readme_stars table --->
196+
197+
| Day | Part 1 | Part 2 |
198+
| :------------------------------------------: | :----: | :----: |
199+
| [Day 1](https://adventofcode.com/2025/day/1) |||
200+
201+
<!--- advent_readme_stars table --->
202+
203+
## Benchmarks
204+
205+
<!--- benchmarking table --->
206+
207+
| Day | Part 1 | Part 2 |
208+
| :----------------------: | :------: | :------: |
209+
| [Day 1](./src/bin/01.rs) | `82.6µs` | `93.5µs` |
210+
211+
**Total: 0.18ms**
212+
213+
<!--- benchmarking table --->

data/examples/01.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
L68
2+
L30
3+
R48
4+
L5
5+
R60
6+
L55
7+
L1
8+
L99
9+
R14
10+
L82

src/bin/.keep

Whitespace-only changes.

src/bin/01.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
advent_of_code::solution!(1);
2+
3+
pub fn part_one(input: &str) -> Option<u64> {
4+
let mut position: i64 = 50;
5+
let mut result: u64 = 0;
6+
for turn in input.lines() {
7+
let (direction, distance) = parse_rotation(turn)?;
8+
9+
position = apply_rotation(position, direction, distance);
10+
11+
if position == 0 {
12+
result += 1;
13+
}
14+
}
15+
16+
Some(result)
17+
}
18+
19+
pub fn part_two(input: &str) -> Option<u64> {
20+
let mut position: i64 = 50;
21+
let mut result: u64 = 0;
22+
23+
for turn in input.lines() {
24+
let (direction, distance) = parse_rotation(turn)?;
25+
26+
// Count complete loops (each loop crosses 0 once)
27+
let complete_loops = distance / 100;
28+
result += complete_loops as u64;
29+
30+
// Check if the partial rotation crosses 0
31+
let remaining = distance % 100;
32+
let crosses_zero = match direction {
33+
'R' => position + remaining >= 100,
34+
'L' => position - remaining <= 0 && position > 0,
35+
_ => false,
36+
};
37+
38+
if crosses_zero {
39+
result += 1;
40+
}
41+
42+
// Update position for next iteration
43+
position = apply_rotation(position, direction, distance);
44+
}
45+
46+
Some(result)
47+
}
48+
49+
fn apply_rotation(position: i64, direction: char, distance: i64) -> i64 {
50+
match direction {
51+
'R' => (position + distance).rem_euclid(100),
52+
'L' => (position - distance).rem_euclid(100),
53+
_ => position,
54+
}
55+
}
56+
57+
fn parse_rotation(line: &str) -> Option<(char, i64)> {
58+
let direction = line.chars().next()?;
59+
let distance: i64 = line[1..].parse().ok()?;
60+
Some((direction, distance))
61+
}
62+
63+
#[cfg(test)]
64+
mod tests {
65+
use super::*;
66+
67+
#[test]
68+
fn test_part_one() {
69+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
70+
assert_eq!(result, Some(3));
71+
}
72+
73+
#[test]
74+
fn test_part_two() {
75+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
76+
assert_eq!(result, Some(6));
77+
}
78+
}

0 commit comments

Comments
 (0)