use super::Solution; pub struct Day01 {} impl Solution for Day01 { fn part1( &self, input: &mut Vec, ) -> Result, Box> { Ok(Box::new(Day01::count_zeros(input, false))) } fn part2( &self, input: &mut Vec, ) -> Result, Box> { Ok(Box::new(Day01::count_zeros(input, true))) } fn get_day(&self) -> u8 { 1 } } impl Day01 { fn count_zeros(input: &Vec, count_passing: bool) -> usize { // Dial starts at 50 let mut pos: i32 = 50; let mut zeros: usize = 0; for line in input { let line = line.trim(); if line.is_empty() { continue; } let mut chars = line.chars(); let dir = chars.next().expect("empty line"); let dist_str: String = chars.collect::().trim().to_string(); let dist: i32 = dist_str.parse().expect("invalid distance number"); // Count any time that the dial moves past 0. if count_passing { // Count all zeros passed during the move let step = match dir { 'L' | 'l' => -1, 'R' | 'r' => 1, _ => panic!("invalid direction (must start with L or R): {}", dir), }; for _ in 0..dist { pos = (pos + step).rem_euclid(100); if pos == 0 { zeros += 1; } } continue; } // Move directly to the new position match dir { 'L' | 'l' => { pos = (pos - dist).rem_euclid(100); } 'R' | 'r' => { pos = (pos + dist).rem_euclid(100); } _ => panic!("invalid direction (must start with L or R): {}", dir), } if pos == 0 { zeros += 1; } } zeros } } /// Test from puzzle input #[cfg(test)] mod test { use super::*; use crate::*; #[test] fn part1() { let challenge = day01::Day01 {}; //Complete the Challenge let answer = challenge .part1( utils::get_input(challenge.get_day(), utils::InputType::Test1) .unwrap() .as_mut(), ) .unwrap() .to_string(); assert_eq!(answer, "3"); } #[test] fn part2() { let challenge = day01::Day01 {}; //Complete the Challenge let answer = challenge .part2( utils::get_input(challenge.get_day(), utils::InputType::Test2) .unwrap() .as_mut(), ) .unwrap() .to_string(); assert_eq!(answer, "6"); } }