Skip to content

Commit 3d11399

Browse files
committed
chor(01/2025): add debug; and more tests
1 parent cb6f198 commit 3d11399

File tree

1 file changed

+77
-24
lines changed

1 file changed

+77
-24
lines changed

src/solutions/year2025/day01.rs

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::solutions::Solution;
2+
use std::fmt::{Display, Formatter};
23
use std::str::FromStr;
34

45
pub struct Day01;
@@ -31,7 +32,7 @@ impl Day01 {
3132
}
3233

3334
struct SafeDial {
34-
value: u16,
35+
position: u16,
3536
zero_stops: u16,
3637
zero_clicks: u16,
3738
}
@@ -41,38 +42,39 @@ impl SafeDial {
4142

4243
fn new() -> Self {
4344
Self {
44-
value: 50,
45+
position: 50,
4546
zero_stops: 0,
4647
zero_clicks: 0,
4748
}
4849
}
4950

5051
fn rotate(&self, rotation: Rotation) -> Self {
51-
let new_value: i16 = match rotation.direction {
52-
Direction::Left => self.value as i16 - rotation.distance as i16,
53-
Direction::Right => self.value as i16 + rotation.distance as i16,
52+
let diff: i16 = match rotation.direction {
53+
Direction::Left => self.position as i16 - rotation.distance as i16,
54+
Direction::Right => self.position as i16 + rotation.distance as i16,
5455
};
5556

5657
let mut zero_stops = self.zero_stops;
5758
let mut zero_clicks = self.zero_clicks;
5859

59-
let value = new_value.rem_euclid(Self::DIAL_NUMBERS_COUNT) as u16;
60-
if value == 0 {
60+
let new_position = diff.rem_euclid(Self::DIAL_NUMBERS_COUNT) as u16;
61+
if new_position == 0 {
6162
zero_stops += 1;
6263
}
6364

64-
let new_zero_clicks = new_value
65-
.div_euclid(Self::DIAL_NUMBERS_COUNT)
66-
.unsigned_abs();
65+
let mut new_zero_clicks = diff.div_euclid(Self::DIAL_NUMBERS_COUNT).unsigned_abs();
66+
67+
if (new_position == 0 || self.position == 0) && new_zero_clicks > 0 {
68+
new_zero_clicks -= 1;
69+
}
6770

6871
zero_clicks += new_zero_clicks;
6972

70-
if (value == 0 || self.value == 0) && new_zero_clicks > 0 {
71-
zero_clicks -= 1;
72-
}
73+
// println!("{} + {} = {}, click: {}, stops: {}", self.position, rotation, new_position, zero_clicks, zero_stops);
74+
// let _ = std::io::stdin().read_line(&mut String::new());
7375

7476
Self {
75-
value,
77+
position: new_position,
7678
zero_stops,
7779
zero_clicks,
7880
}
@@ -85,6 +87,15 @@ impl SafeDial {
8587
fn points_to_zero(&self) -> u16 {
8688
self.zero_clicks + self.zero_stops
8789
}
90+
91+
#[cfg(test)]
92+
fn with(value: u16) -> Self {
93+
Self {
94+
position: value,
95+
zero_stops: 0,
96+
zero_clicks: 0,
97+
}
98+
}
8899
}
89100

90101
#[derive(Debug, PartialEq)]
@@ -93,6 +104,15 @@ enum Direction {
93104
Right,
94105
}
95106

107+
impl Display for Direction {
108+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
109+
match self {
110+
Direction::Left => write!(f, "L"),
111+
Direction::Right => write!(f, "R"),
112+
}
113+
}
114+
}
115+
96116
#[derive(Debug, PartialEq)]
97117
struct Rotation {
98118
direction: Direction,
@@ -136,6 +156,12 @@ impl FromStr for Rotation {
136156
}
137157
}
138158

159+
impl Display for Rotation {
160+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
161+
write!(f, "{}{}", self.direction, self.distance)
162+
}
163+
}
164+
139165
#[cfg(test)]
140166
mod tests {
141167
use crate::solutions::year2025::day01::{Day01, Rotation, SafeDial};
@@ -174,56 +200,74 @@ L82"#;
174200
let mut dial = SafeDial::new();
175201

176202
dial = dial.rotate(Rotation::left(68));
177-
assert_eq!(82, dial.value);
203+
assert_eq!(82, dial.position);
178204
assert_eq!(0, dial.zero_stops);
179205
assert_eq!(1, dial.zero_clicks);
180206

181207
dial = dial.rotate(Rotation::left(30));
182-
assert_eq!(52, dial.value);
208+
assert_eq!(52, dial.position);
183209
assert_eq!(0, dial.zero_stops);
184210
assert_eq!(1, dial.zero_clicks);
185211

186212
dial = dial.rotate(Rotation::right(48));
187-
assert_eq!(0, dial.value);
213+
assert_eq!(0, dial.position);
188214
assert_eq!(1, dial.zero_stops);
189215
assert_eq!(1, dial.zero_clicks);
190216

191217
dial = dial.rotate(Rotation::left(5));
192-
assert_eq!(95, dial.value);
218+
assert_eq!(95, dial.position);
193219
assert_eq!(1, dial.zero_stops);
194220
assert_eq!(1, dial.zero_clicks);
195221

196222
dial = dial.rotate(Rotation::right(60));
197-
assert_eq!(55, dial.value);
223+
assert_eq!(55, dial.position);
198224
assert_eq!(1, dial.zero_stops);
199225
assert_eq!(2, dial.zero_clicks);
200226

201227
dial = dial.rotate(Rotation::left(55));
202-
assert_eq!(0, dial.value);
228+
assert_eq!(0, dial.position);
203229
assert_eq!(2, dial.zero_stops);
204230
assert_eq!(2, dial.zero_clicks);
205231

206232
dial = dial.rotate(Rotation::left(1));
207-
assert_eq!(99, dial.value);
233+
assert_eq!(99, dial.position);
208234
assert_eq!(2, dial.zero_stops);
209235
assert_eq!(2, dial.zero_clicks);
210236

211237
dial = dial.rotate(Rotation::left(99));
212-
assert_eq!(0, dial.value);
238+
assert_eq!(0, dial.position);
213239
assert_eq!(3, dial.zero_stops);
214240
assert_eq!(2, dial.zero_clicks);
215241

216242
dial = dial.rotate(Rotation::right(14));
217-
assert_eq!(14, dial.value);
243+
assert_eq!(14, dial.position);
218244
assert_eq!(3, dial.zero_stops);
219245
assert_eq!(2, dial.zero_clicks);
220246

221247
dial = dial.rotate(Rotation::left(82));
222-
assert_eq!(32, dial.value);
248+
assert_eq!(32, dial.position);
223249
assert_eq!(3, dial.zero_stops);
224250
assert_eq!(3, dial.zero_clicks);
225251
}
226252

253+
#[test]
254+
fn my_input_tests() {
255+
let mut dial = SafeDial::with(90);
256+
dial = dial.rotate(Rotation::left(636));
257+
// 1 -100:90, 536
258+
// 2 -100:90, 436
259+
// 3 -100:90, 336
260+
// 4 -100:90, 236
261+
// 5 -100:90, 136
262+
// 6 -100:90, 36
263+
// 6 -36:54
264+
265+
assert_eq!(6, dial.points_to_zero());
266+
267+
dial = dial.rotate(Rotation::left(654));
268+
assert_eq!(12, dial.points_to_zero());
269+
}
270+
227271
#[test]
228272
fn zero_clicks_test() {
229273
let mut dial = SafeDial::new();
@@ -254,6 +298,15 @@ L82"#;
254298

255299
dial = dial.rotate(Rotation::left(98)); // 1
256300
assert_eq!(16, dial.points_to_zero());
301+
302+
dial = dial.rotate(Rotation::right(199)); // 0
303+
assert_eq!(18, dial.points_to_zero());
304+
305+
dial = dial.rotate(Rotation::right(1)); // 1
306+
assert_eq!(18, dial.points_to_zero());
307+
308+
dial = dial.rotate(Rotation::left(102)); // 99
309+
assert_eq!(20, dial.points_to_zero());
257310
}
258311

259312
#[test]

0 commit comments

Comments
 (0)