Files
AdventOfCode2025/src/solutions/day01.rs
Luke Else 0dee050ef2
Some checks failed
Continuous integration / Check (push) Successful in 44s
Continuous integration / Test Suite (push) Successful in 44s
Continuous integration / Rustfmt (push) Successful in 31s
Continuous integration / Clippy (push) Failing after 44s
Continuous integration / build (push) Successful in 44s
feat: Completed day 2code .
2025-12-02 10:24:07 +00:00

119 lines
3.0 KiB
Rust

use super::Solution;
pub struct Day01 {}
impl Solution for Day01 {
fn part1(
&self,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
Ok(Box::new(Day01::count_zeros(input, false)))
}
fn part2(
&self,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
Ok(Box::new(Day01::count_zeros(input, true)))
}
fn get_day(&self) -> u8 {
1
}
}
impl Day01 {
fn count_zeros(input: &Vec<String>, 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::<String>().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");
}
}