Skip to content

Commit ce016e6

Browse files
committed
AoC 2025 Day 1 - rust
1 parent b9ea472 commit ce016e6

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| python3 | [](src/main/python/AoC2025_01.py) | [](src/main/python/AoC2025_02.py) | [](src/main/python/AoC2025_03.py) | [](src/main/python/AoC2025_04.py) | [](src/main/python/AoC2025_05.py) | [](src/main/python/AoC2025_06.py) | [](src/main/python/AoC2025_07.py) | [](src/main/python/AoC2025_08.py) | [](src/main/python/AoC2025_09.py) | [](src/main/python/AoC2025_10.py) | [](src/main/python/AoC2025_11.py) | [](src/main/python/AoC2025_12.py) |
1414
| java | [](src/main/java/AoC2025_01.java) | [](src/main/java/AoC2025_02.java) | [](src/main/java/AoC2025_03.java) | [](src/main/java/AoC2025_04.java) | [](src/main/java/AoC2025_05.java) | [](src/main/java/AoC2025_06.java) | [](src/main/java/AoC2025_07.java) | [](src/main/java/AoC2025_08.java) | [](src/main/java/AoC2025_09.java) | [](src/main/java/AoC2025_10.java) | [](src/main/java/AoC2025_11.java) | [](src/main/java/AoC2025_12.java) |
1515
| bash | [](src/main/bash/AoC2025_01.sh) | [](src/main/bash/AoC2025_02.sh) | [](src/main/bash/AoC2025_03.sh) | [](src/main/bash/AoC2025_04.sh) | [](src/main/bash/AoC2025_05.sh) | [](src/main/bash/AoC2025_06.sh) | [](src/main/bash/AoC2025_07.sh) | | | | [](src/main/bash/AoC2025_11.sh) | [](src/main/bash/AoC2025_12.sh) |
16+
| rust | [](src/main/rust/AoC2025_01/src/main.rs) | | | | | | | | | | | |
1617
<!-- @END:ImplementationsTable:2025@ -->
1718

1819
## 2024
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "AoC2025_01"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
aoc = { path = "../aoc" }
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#![allow(non_snake_case)]
2+
3+
use aoc::Puzzle;
4+
5+
const START: isize = 50;
6+
const TOTAL: isize = 100;
7+
8+
enum Count {
9+
LandedOnZero,
10+
PassedByZero,
11+
}
12+
13+
impl Count {
14+
fn count(&self, dial: isize, rotation: isize) -> usize {
15+
match self {
16+
Count::LandedOnZero => {
17+
if (dial + rotation) % TOTAL == 0 {
18+
1
19+
} else {
20+
0
21+
}
22+
}
23+
Count::PassedByZero => {
24+
if rotation >= 0 {
25+
((dial + rotation) / TOTAL) as usize
26+
} else {
27+
(((TOTAL - dial) % TOTAL - rotation) / TOTAL) as usize
28+
}
29+
}
30+
}
31+
}
32+
}
33+
34+
struct AoC2025_01;
35+
36+
impl AoC2025_01 {
37+
fn solve(&self, rotations: &[isize], count: Count) -> usize {
38+
let mut dial = START;
39+
let mut ans = 0;
40+
rotations.iter().for_each(|rotation| {
41+
ans += count.count(dial, *rotation);
42+
dial = (dial + *rotation).rem_euclid(TOTAL);
43+
});
44+
ans
45+
}
46+
}
47+
48+
impl aoc::Puzzle for AoC2025_01 {
49+
type Input = Vec<isize>;
50+
type Output1 = usize;
51+
type Output2 = usize;
52+
53+
aoc::puzzle_year_day!(2025, 1);
54+
55+
fn parse_input(&self, lines: Vec<String>) -> Self::Input {
56+
lines
57+
.iter()
58+
.map(|line| {
59+
let s = match line.chars().nth(0).unwrap() {
60+
'R' => 1,
61+
'L' => -1,
62+
_ => panic!(),
63+
};
64+
s * line[1..].parse::<isize>().unwrap()
65+
})
66+
.collect::<Vec<_>>()
67+
}
68+
69+
fn part_1(&self, rotations: &Self::Input) -> Self::Output1 {
70+
self.solve(rotations, Count::LandedOnZero)
71+
}
72+
73+
fn part_2(&self, rotations: &Self::Input) -> Self::Output2 {
74+
self.solve(rotations, Count::PassedByZero)
75+
}
76+
77+
fn samples(&self) {
78+
aoc::puzzle_samples! {
79+
self, part_1, TEST, 3,
80+
self, part_2, TEST, 6
81+
};
82+
}
83+
}
84+
85+
fn main() {
86+
AoC2025_01 {}.run(std::env::args());
87+
}
88+
89+
const TEST: &str = "\
90+
L68
91+
L30
92+
R48
93+
L5
94+
R60
95+
L55
96+
L1
97+
L99
98+
R14
99+
L82
100+
";
101+
102+
#[cfg(test)]
103+
mod tests {
104+
use super::*;
105+
106+
#[test]
107+
pub fn samples() {
108+
AoC2025_01 {}.samples();
109+
}
110+
}

src/main/rust/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)