Skip to content

Commit e9857b2

Browse files
committed
feat(01/2025): solve first part
1 parent fa08a4e commit e9857b2

File tree

2 files changed

+135
-5
lines changed

2 files changed

+135
-5
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
| Day | Solved | Part 1 time (ms) | Part 2 time (ms) |
1313
|-----------------------------------------------------------|:------:|-----------------:|-----------------:|
14-
| [Day 1: Secret Entrance](src/solutions/year2025/day01.rs) | | - | - |
14+
| [Day 1: Secret Entrance](src/solutions/year2025/day01.rs) | | 0.144 | - |
1515

1616
# 2024
1717

src/solutions/year2025/day01.rs

Lines changed: 134 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,156 @@
11
use crate::solutions::Solution;
2+
use std::str::FromStr;
23

34
pub struct Day01;
45

56
impl Solution for Day01 {
67
fn part_one(&self, _input: &str) -> String {
7-
String::from("")
8+
let input = self.parse(_input);
9+
let mut dial = SafeDial::new();
10+
let mut password: u16 = 0;
11+
12+
for rotation in input {
13+
dial = dial.rotate(rotation);
14+
if dial.is_zero() {
15+
password += 1;
16+
}
17+
}
18+
19+
password.to_string()
820
}
921

1022
fn part_two(&self, _input: &str) -> String {
1123
String::from("")
1224
}
1325
}
1426

27+
impl Day01 {
28+
fn parse(&self, input: &str) -> Vec<Rotation> {
29+
input.lines().map(|line| line.parse().unwrap()).collect()
30+
}
31+
}
32+
33+
struct SafeDial {
34+
value: u16,
35+
}
36+
37+
impl SafeDial {
38+
fn new() -> Self {
39+
Self { value: 50 }
40+
}
41+
42+
fn rotate(&self, rotation: Rotation) -> Self {
43+
const DIAL_NUMBERS_COUNT: i16 = 100;
44+
45+
let new_value: i16 = match rotation.direction {
46+
Direction::Left => self.value as i16 - rotation.distance as i16,
47+
Direction::Right => self.value as i16 + rotation.distance as i16,
48+
};
49+
50+
Self {
51+
value: new_value.rem_euclid(DIAL_NUMBERS_COUNT) as u16,
52+
}
53+
}
54+
55+
fn is_zero(&self) -> bool {
56+
self.value == 0
57+
}
58+
}
59+
60+
#[derive(Debug, PartialEq)]
61+
enum Direction {
62+
Left,
63+
Right,
64+
}
65+
66+
#[derive(Debug, PartialEq)]
67+
struct Rotation {
68+
direction: Direction,
69+
distance: u16,
70+
}
71+
72+
#[cfg(test)]
73+
impl Rotation {
74+
fn new(direction: Direction, distance: u16) -> Self {
75+
Self {
76+
direction,
77+
distance,
78+
}
79+
}
80+
81+
fn left(distance: u16) -> Self {
82+
Self::new(Direction::Left, distance)
83+
}
84+
85+
fn right(distance: u16) -> Self {
86+
Self::new(Direction::Right, distance)
87+
}
88+
}
89+
90+
impl FromStr for Rotation {
91+
type Err = String;
92+
93+
fn from_str(s: &str) -> Result<Self, Self::Err> {
94+
let direction = match s.chars().next() {
95+
Some('L') => Direction::Left,
96+
Some('R') => Direction::Right,
97+
_ => return Err(String::from("Invalid direction")),
98+
};
99+
100+
let distance = s[1..].parse::<u16>().unwrap();
101+
102+
Ok(Self {
103+
direction,
104+
distance,
105+
})
106+
}
107+
}
108+
15109
#[cfg(test)]
16110
mod tests {
17-
use crate::solutions::year2025::day01::Day01;
111+
use crate::solutions::year2025::day01::{Day01, Rotation, SafeDial};
18112
use crate::solutions::Solution;
19113

20-
const EXAMPLE: &str = r#""#;
114+
const EXAMPLE: &str = r#"L68
115+
L30
116+
R48
117+
L5
118+
R60
119+
L55
120+
L1
121+
L99
122+
R14
123+
L82"#;
21124

22125
#[test]
23126
fn part_one_example_test() {
24-
assert_eq!("", Day01.part_one(EXAMPLE));
127+
assert_eq!("3", Day01.part_one(EXAMPLE));
128+
}
129+
130+
#[test]
131+
fn rotation_parse_test() {
132+
assert_eq!("L30".parse::<Rotation>().unwrap(), Rotation::left(30));
133+
assert_eq!("R48".parse::<Rotation>().unwrap(), Rotation::right(48));
134+
}
135+
136+
#[test]
137+
fn safe_dial_rotate_test() {
138+
let mut dial = SafeDial::new();
139+
140+
dial = dial.rotate(Rotation::left(68));
141+
assert_eq!(82, dial.value);
142+
assert!(!dial.is_zero());
143+
144+
dial = dial.rotate(Rotation::left(30));
145+
assert_eq!(52, dial.value);
146+
assert!(!dial.is_zero());
147+
148+
dial = dial.rotate(Rotation::right(48));
149+
assert_eq!(0, dial.value);
150+
assert!(dial.is_zero());
151+
152+
dial = dial.rotate(Rotation::left(5));
153+
assert_eq!(95, dial.value);
154+
assert!(!dial.is_zero());
25155
}
26156
}

0 commit comments

Comments
 (0)