From da6f15140252ff6619f0a52b664b4adba99428b6 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Tue, 2 Dec 2025 23:28:48 -0500 Subject: [PATCH] Day 1 Finished --- crates/day_1/src/day_1.rs | 141 ++++++++++++++++++++++++++++-------- crates/solver/src/solver.rs | 5 ++ src/main.rs | 5 +- 3 files changed, 118 insertions(+), 33 deletions(-) diff --git a/crates/day_1/src/day_1.rs b/crates/day_1/src/day_1.rs index 24f2471..af09b1a 100644 --- a/crates/day_1/src/day_1.rs +++ b/crates/day_1/src/day_1.rs @@ -7,6 +7,8 @@ * @brief pre-advent testing ******************************************************************************/ +// use std::path::absolute; + use solver::solver::{Solver, SolverOption, SolverState}; use utils::utils; @@ -18,6 +20,7 @@ pub struct Day1 input: String, dial_position: i32, rotations: Vec, + // full_rots: Vec, zeros: u64, } @@ -40,34 +43,70 @@ impl Solver for Day1 fn solve(&mut self, _input: String) -> Result { - let verbose = self.solver_state.check_option(SolverOption::Verbose); + // let verbose = self.solver_state.check_option(SolverOption::Verbose); + // let count_passes = self.solver_state.get_value("count_passing_zeros") == "true"; println!("Starting Dial Pos: {}", self.dial_position); println!("Number of rotations: {}", self.rotations.len()); for rot in &self.rotations { - self.dial_position += rot; - if self.dial_position < 0 - { - self.dial_position = DIAL_MAX_POS + self.dial_position + 1; - } - else if self.dial_position > DIAL_MAX_POS - { - self.dial_position -= DIAL_MAX_POS + 1; - } - - if verbose - { - println!("Applied rotation {}, dial pos: {}", rot, self.dial_position); - } - - if 0 == self.dial_position - { - self.zeros += 1; - if verbose - { - println!("Zero found, total: {}", self.zeros); - } - } + let (new_pos, num_zeros) = Day1::process_rot(self.dial_position, *rot); + self.dial_position = new_pos; + self.zeros += num_zeros; + + // self.dial_position += rot; + // if verbose + // { + // println!("Applied rotation {}", rot); + // } + + // if self.dial_position < 0 + // { + // if count_passes && (self.dial_position - rot != 0) + // { + // if verbose && self.dial_position != 0 && self.dial_position != 0 + // { + // println!("Counting a passing zero"); + // } + // self.zeros += 1; + // } + // self.dial_position = DIAL_MAX_POS + self.dial_position + 1; + + // } + // else if self.dial_position > DIAL_MAX_POS + // { + // self.dial_position -= DIAL_MAX_POS + 1; + // if count_passes && self.dial_position != 0 + // { + // if verbose + // { + // println!("Counting a passing zero"); + // } + // self.zeros += 1; + // } + // } + + // if count_passes + // { + // if verbose && self.full_rots[i] as u64 > 0 + // { + // println!("Counting extra rotation zeros: {}", self.full_rots[i]); + // } + // self.zeros += self.full_rots[i] as u64; + // } + + // if verbose + // { + // println!("New dial pos: {}", self.dial_position); + // } + + // if 0 == self.dial_position + // { + // self.zeros += 1; + // if verbose + // { + // println!("Zero found, total: {}", self.zeros); + // } + // } } Ok(self.zeros) } @@ -82,7 +121,8 @@ impl Day1 { pub fn new() -> Day1 { - Day1 { solver_state: SolverState::new(), input: "".to_string(), dial_position: 50, rotations: vec![], zeros: 0 } + Day1 { solver_state: SolverState::new(), input: "".to_string(), dial_position: 50, + rotations: vec![], zeros: 0 } } fn parse_rots(&mut self) @@ -99,22 +139,61 @@ impl Day1 // NOTE: Trusting there are no errors in the input file! let trimmed = &line[1..]; - let mut mag: i32 = trimmed.parse().expect(&format!("Failed to parse rotation value: {}", trimmed)); + let mag: i32 = trimmed.parse().expect(&format!("Failed to parse rotation value: {}", trimmed)); // mag = mag % DIAL_MAX_POS; - if mag > DIAL_MAX_POS - { - mag %= DIAL_MAX_POS + 1 - } + // if mag > DIAL_MAX_POS + // { + // let num_full_rots = mag / DIAL_MAX_POS; + // mag %= DIAL_MAX_POS + 1; + // self.full_rots.push(num_full_rots); + // } + // else + // { + // self.full_rots.push(0); + // } let rot = mag * coef; self.rotations.push(rot); // println!("as: {}", rot); } } - // fn process_rot(&mut self, ) + /// Returns the new dial_position and number of zeros landed on during this rotation + fn process_rot(dial_pos: i32, rot: i32) -> (i32, u64) // (new dial pos, num zeros passed) + { + let direction = match rot >= 0 + { + true => 1, + false => -1, + }; + + let mag = rot.abs(); + let mut dial_pos = dial_pos; + let mut zeros = 0; + for _ in 0..mag + { + dial_pos += (1 * direction); + if dial_pos == (DIAL_MAX_POS + 1) + { + dial_pos = 0; + } + + if dial_pos == -1 + { + dial_pos = DIAL_MAX_POS; + } + + if dial_pos == 0 + { + zeros += 1; + } + } + + (dial_pos, zeros) + } } + #[allow(dead_code)] fn print_rot(r: i32) { diff --git a/crates/solver/src/solver.rs b/crates/solver/src/solver.rs index 9039eef..0b7e0e8 100644 --- a/crates/solver/src/solver.rs +++ b/crates/solver/src/solver.rs @@ -65,6 +65,11 @@ impl SolverState pub fn get_value(self: &SolverState, key: &str) -> String { + if !self.values.contains_key(key) + { + return "".to_string(); + } + self.values[&key.to_string()].clone() } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f4096d0..cdef8e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,9 +41,10 @@ fn run_test_day() fn run_day_1() { println!("Running Day 1..."); - let state = SolverState::new(); + let mut state = SolverState::new(); // state.set_option(SolverOption::Debug); - // state.set_option(SolverOption::Verbose); + state.set_option(SolverOption::Verbose); + state.set_value("true", "count_passing_zeros"); let mut day1 = Day1::new(); day1.init(state).expect(&format!("Day {} failed to init", day1.name()));