|
| 1 | +// Copyright (c) 2025 Lexi Robinson |
| 2 | +// |
| 3 | +// Licensed under the EUPL, Version 1.2 |
| 4 | +// |
| 5 | +// You may not use this work except in compliance with the Licence. |
| 6 | +// You should have received a copy of the Licence along with this work. If not, see: |
| 7 | +// <https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12>. |
| 8 | +// See the Licence for the specific language governing permissions and limitations under the Licence. |
| 9 | + |
| 10 | +use std::fmt::Display; |
| 11 | +use std::str::FromStr; |
| 12 | + |
| 13 | +use crate::AoCError; |
| 14 | + |
| 15 | +#[derive(Debug)] |
| 16 | +enum Instruction { |
| 17 | + Left(i16), |
| 18 | + Right(i16), |
| 19 | +} |
| 20 | + |
| 21 | +impl FromStr for Instruction { |
| 22 | + type Err = AoCError; |
| 23 | + |
| 24 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 25 | + let (dir, count) = s |
| 26 | + .split_at_checked(1) |
| 27 | + .ok_or(AoCError::new("Unexpected utf sequence"))?; |
| 28 | + |
| 29 | + let count = count.parse().map_err(AoCError::new_from_parseerror)?; |
| 30 | + |
| 31 | + Ok(match dir { |
| 32 | + "L" => Self::Left(count), |
| 33 | + "R" => Self::Right(count), |
| 34 | + c => return Err(AoCError::new(format!("Unexpected direction {c}!"))), |
| 35 | + }) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl Display for Instruction { |
| 40 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 41 | + write!( |
| 42 | + f, |
| 43 | + "Rotate {} {} clicks", |
| 44 | + match self { |
| 45 | + Self::Left(_) => "left", |
| 46 | + Self::Right(_) => "right", |
| 47 | + }, |
| 48 | + match self { |
| 49 | + Self::Left(i) => i, |
| 50 | + Self::Right(i) => i, |
| 51 | + } |
| 52 | + ) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#[derive(Debug)] |
| 57 | +struct Dial(i64); |
| 58 | + |
| 59 | +impl Default for Dial { |
| 60 | + fn default() -> Self { |
| 61 | + Self(50) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl Display for Dial { |
| 66 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 67 | + write!(f, "Dial is at {}", self.0) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl Dial { |
| 72 | + fn rotate(&mut self, instruction: Instruction) { |
| 73 | + match instruction { |
| 74 | + Instruction::Left(count) => { |
| 75 | + let count: i64 = count.into(); |
| 76 | + self.0 -= count; |
| 77 | + } |
| 78 | + Instruction::Right(count) => { |
| 79 | + let count: i64 = count.into(); |
| 80 | + self.0 += count; |
| 81 | + } |
| 82 | + } |
| 83 | + self.clampinate(); |
| 84 | + } |
| 85 | + |
| 86 | + fn clampinate(&mut self) { |
| 87 | + while self.0 < 0 { |
| 88 | + self.0 += 100; |
| 89 | + } |
| 90 | + while self.0 > 99 { |
| 91 | + self.0 -= 100; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +pub fn part_1(data: crate::DataIn) -> crate::AoCResult<String> { |
| 97 | + let mut ret = 0; |
| 98 | + let mut dial: Dial = Default::default(); |
| 99 | + for line in data { |
| 100 | + let instruction = line.parse()?; |
| 101 | + // println!("{dial}\n{instruction}"); |
| 102 | + dial.rotate(instruction); |
| 103 | + if dial.0 == 0 { |
| 104 | + ret += 1; |
| 105 | + } |
| 106 | + } |
| 107 | + // println!("{dial}"); |
| 108 | + Ok(ret.to_string()) |
| 109 | +} |
| 110 | + |
| 111 | +inventory::submit!(crate::AoCDay { |
| 112 | + year: "2025", |
| 113 | + day: "1", |
| 114 | + part_1: crate::AoCPart { |
| 115 | + main: part_1, |
| 116 | + example: part_1 |
| 117 | + }, |
| 118 | + part_2: None |
| 119 | +}); |
0 commit comments