From c1c963c7fbea06b0b2fc1031272c927b8f791ab8 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Tue, 2 Dec 2025 01:18:13 -0500 Subject: [PATCH] working on day 1 --- Cargo.lock | 9 ++ Cargo.toml | 6 +- build.rs | 33 +++++- crates/day_0/src/day_0.rs | 7 +- crates/day_1/Cargo.toml | 9 ++ crates/day_1/build.rs | 8 ++ crates/day_1/src/day_1.rs | 109 ++++++++++++++++++ crates/day_1/src/lib.rs | 2 + crates/day_0/input/input => input/day_0_input | 0 .../input_test => input/day_0_test_input | 0 input/day_1_test_input | 10 ++ src/main.rs | 39 ++++++- 12 files changed, 224 insertions(+), 8 deletions(-) create mode 100644 crates/day_1/Cargo.toml create mode 100644 crates/day_1/build.rs create mode 100644 crates/day_1/src/day_1.rs create mode 100644 crates/day_1/src/lib.rs rename crates/day_0/input/input => input/day_0_input (100%) rename crates/day_0/input/input_test => input/day_0_test_input (100%) create mode 100644 input/day_1_test_input diff --git a/Cargo.lock b/Cargo.lock index da976ca..d31ae9c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ name = "adventofcode2025" version = "0.1.0" dependencies = [ "day_0", + "day_1", "solver", "utils", ] @@ -19,6 +20,14 @@ dependencies = [ "utils", ] +[[package]] +name = "day_1" +version = "0.1.0" +dependencies = [ + "solver", + "utils", +] + [[package]] name = "solver" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 65660d7..8a68ab3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ members = [ "crates/utils", "crates/solver", "crates/day_0", - # "crates/day_1", + "crates/day_1", # "crates/day_2", # "crates/day_3", # "crates/day_4", @@ -23,7 +23,7 @@ members = [ utils = { workspace = true } solver = { workspace = true } day_0 = { workspace = true } -# day_1 = { workspace = true } +day_1 = { workspace = true } # day_2 = { workspace = true } # day_3 = { workspace = true } # day_4 = { workspace = true } @@ -37,7 +37,7 @@ day_0 = { workspace = true } utils = { path = "crates/utils" } solver = { path = "crates/solver" } day_0 = { path = "crates/day_0" } -# day_1 = { path = "crates/day_1" } +day_1 = { path = "crates/day_1" } # day_2 = { path = "crates/day_2" } # day_3 = { path = "crates/day_3" } # day_4 = { path = "crates/day_4" } diff --git a/build.rs b/build.rs index 262b9b7..ea4becd 100644 --- a/build.rs +++ b/build.rs @@ -8,13 +8,44 @@ ******************************************************************************/ // use std::process::Command; +use std::io::prelude::*; +use std::{env, fs, fs::File, path::Path}; fn main() { let name = env!("CARGO_PKG_NAME"); println!("{} build.rs started...", name); + let in_path = Path::new("input/"); + let out_path = format!("target/{}", &env::var("PROFILE").unwrap()); + let out_path = Path::new(&out_path).join("input/"); + let test_file_name = out_path.join("foo.txt".to_string()); - + if !out_path.exists() + { + fs::create_dir(&out_path).expect("Failed to create the out directory"); + } + + if !in_path.exists() + { + fs::create_dir(&in_path).expect("failed to create dummy input path"); + } + + if !in_path.is_dir() + { + println!("in_path is not a directory"); + } + + // Test file + let mut file = File::create(test_file_name).expect("Failed to create test file"); + file.write_all(b"Hello, world!").expect("Failed to write to test file"); + + // Copy input files + for f in in_path.read_dir().expect("Failed to parse directory entries") + { + let f = f.expect("failed to parse directly entry"); + let dest = out_path.join(f.path().file_name().unwrap()); + fs::copy(f.path(), &dest).expect(&format!("failed to copy file: {:#?}, to {:#?}", f, dest)); + } println!("{} build.rs finished", name); } \ No newline at end of file diff --git a/crates/day_0/src/day_0.rs b/crates/day_0/src/day_0.rs index 4dab139..5a495b9 100644 --- a/crates/day_0/src/day_0.rs +++ b/crates/day_0/src/day_0.rs @@ -8,11 +8,13 @@ ******************************************************************************/ use solver::solver::{Solver, SolverState}; +use utils::utils; pub struct Day0 { solver_state: SolverState, num_fib_values: i32, + input: String, } impl Solver for Day0 @@ -21,11 +23,14 @@ impl Solver for Day0 { self.solver_state = _config.clone(); self.num_fib_values = self.solver_state.get_value("num_fib_values").parse().expect("Could not parse num_fib_values"); + self.input = utils::read_text_file("input/day_0_test_input"); return Ok(()) } fn solve(&mut self, _input: String) -> Result { + println!("args are: {:#?}", self.solver_state); + println!("input is: {}", self.input); let fibs = gen_fib_seq(self.num_fib_values, std::i64::MAX); // no max_val basically println!("fib nums:"); print_fibs(fibs); @@ -42,7 +47,7 @@ impl Day0 { pub fn new() -> Day0 { - Day0 { solver_state: SolverState::new(), num_fib_values: 5 } + Day0 { solver_state: SolverState::new(), num_fib_values: 5, input: "".to_string() } } } diff --git a/crates/day_1/Cargo.toml b/crates/day_1/Cargo.toml new file mode 100644 index 0000000..a79a3d0 --- /dev/null +++ b/crates/day_1/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day_1" +description = "Day 1 of the Advent of Code 2025!" +version = "0.1.0" +edition = "2024" + +[dependencies] +solver = { workspace = true } +utils = { workspace = true } \ No newline at end of file diff --git a/crates/day_1/build.rs b/crates/day_1/build.rs new file mode 100644 index 0000000..9472a20 --- /dev/null +++ b/crates/day_1/build.rs @@ -0,0 +1,8 @@ + +fn main() +{ + let name = env!("CARGO_PKG_NAME"); + println!("{} build.rs started...", name); + + println!("{} build.rs finished...", name); +} \ No newline at end of file diff --git a/crates/day_1/src/day_1.rs b/crates/day_1/src/day_1.rs new file mode 100644 index 0000000..f2446cf --- /dev/null +++ b/crates/day_1/src/day_1.rs @@ -0,0 +1,109 @@ +/****************************************************************************** +* @file day_1.rs +* @author Joey Pollack +* @date 2025/12/01 (y/m/d) +* @modified 2025/12/01 (y/m/d) +* @copyright Joseph R Pollack (2025) +* @brief pre-advent testing +******************************************************************************/ + +use solver::solver::{Solver, SolverOption, SolverState}; +use utils::utils; + +pub struct Day1 +{ + solver_state: SolverState, + input: String, + dial_position: i32, + rotations: Vec, + zeros: u64, +} + +impl Solver for Day1 +{ + fn init(&mut self, config: solver::solver::SolverState) -> Result<(), String> + { + self.solver_state = config; + self.input = utils::read_text_file("input/day_1_test_input"); + self.parse_rots(); + return Ok(()) + } + + fn solve(&mut self, _input: String) -> Result + { + let verbose = self.solver_state.check_option(SolverOption::Verbose); + println!("Starting Dial Pos: {}", self.dial_position); + for rot in &self.rotations + { + self.dial_position += rot; + + 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); + } + } + } + Ok(0) + } + + fn name(&self) -> String + { + "Day 1".to_string() + } +} + +impl Day1 +{ + pub fn new() -> Day1 + { + Day1 { solver_state: SolverState::new(), input: "".to_string(), dial_position: 50, rotations: vec![], zeros: 0 } + } + + fn parse_rots(&mut self) + { + for line in self.input.lines() + { + // print!("Parsed rotation {} ", line); + let mut coef = 1; + if line.starts_with("L") + { + coef = -1; + } + + // NOTE: Trusting there are no errors in the input file! + + let trimmed = &line[1..]; + let mag: i32 = trimmed.parse().expect(&format!("Failed to parse rotation value: {}", trimmed)); + let rot = mag * coef; + self.rotations.push(rot); + // println!("as: {}", rot); + } + } + + // fn process_rot(&mut self, ) +} + +fn print_rot(r: i32) +{ + let mut rot = r; + if r > 0 + { + print!("R") + } + else + { + rot = r * -1; + print!("L") + } + + print!("{}", rot); + +} diff --git a/crates/day_1/src/lib.rs b/crates/day_1/src/lib.rs new file mode 100644 index 0000000..790e68a --- /dev/null +++ b/crates/day_1/src/lib.rs @@ -0,0 +1,2 @@ + +pub mod day_1; \ No newline at end of file diff --git a/crates/day_0/input/input b/input/day_0_input similarity index 100% rename from crates/day_0/input/input rename to input/day_0_input diff --git a/crates/day_0/input/input_test b/input/day_0_test_input similarity index 100% rename from crates/day_0/input/input_test rename to input/day_0_test_input diff --git a/input/day_1_test_input b/input/day_1_test_input new file mode 100644 index 0000000..d03fad7 --- /dev/null +++ b/input/day_1_test_input @@ -0,0 +1,10 @@ +L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 64a20c8..12d5296 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,21 +2,30 @@ use solver::solver::{Solver, SolverState, SolverOption}; use day_0::day_0::Day0; +use day_1::day_1::Day1; +// TODO: proper error handling fn main() { println!("Hello, world!"); + let _ = SolverOption::Debug; // shutup compiler warning, I know. // TODO: Grab command line args - // TODO: Parse args into a SolverState + // TODO: Parse args into a vec of SolverStates, one for each day + + // TODO: Replace these function calls with some code to run days based on the -r argument + run_test_day(); + run_day_1(); +} + +fn run_test_day() +{ let mut state = SolverState::new(); state.set_value("50", "num_fib_values"); - let _ = SolverOption::Debug; let mut test_day = Day0::new(); - // TODO: init test_day with the test_day.init(state).expect(&format!("Test {} Failed to init", test_day.name())); let result = match test_day.solve("".to_string()) @@ -26,5 +35,29 @@ fn main() }; println!("Test {} result: {}", test_day.name(), result); +} + +fn run_day_1() +{ + let state = SolverState::new(); + let mut day1 = Day1::new(); + day1.init(state).expect(&format!("Day {} failed to init", day1.name())); + + let result = match day1.solve("".to_string()) + { + Ok(r) => r, + Err(e) => panic!("Solve Error: {}", e), + }; + + println!("Test {} result: {}", day1.name(), result); } + +/// parse stuff like `-d1 verbose debug` which is: day 1 options: "verbose" and "debug", +/// and `-g debug` which is: global option: "debug". Global options will be added to each day that is run. +/// Specify which days to run with: `-r 1 2 5 9` which will run days 1, 2, 5, and 9. +/// `-r all` will run all days. +fn parse_args() +{ + // TODO: implement parse_args() +} \ No newline at end of file