|
1 | 1 | use crate::solutions::Solution; |
| 2 | +use std::str::FromStr; |
2 | 3 |
|
3 | 4 | pub struct Day01; |
4 | 5 |
|
5 | 6 | impl Solution for Day01 { |
6 | 7 | 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() |
8 | 20 | } |
9 | 21 |
|
10 | 22 | fn part_two(&self, _input: &str) -> String { |
11 | 23 | String::from("") |
12 | 24 | } |
13 | 25 | } |
14 | 26 |
|
| 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 | + |
15 | 109 | #[cfg(test)] |
16 | 110 | mod tests { |
17 | | - use crate::solutions::year2025::day01::Day01; |
| 111 | + use crate::solutions::year2025::day01::{Day01, Rotation, SafeDial}; |
18 | 112 | use crate::solutions::Solution; |
19 | 113 |
|
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"#; |
21 | 124 |
|
22 | 125 | #[test] |
23 | 126 | 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()); |
25 | 155 | } |
26 | 156 | } |
0 commit comments