working on day 1

master
Joey Pollack 1 week ago
parent 0dc3586ff7
commit c1c963c7fb

9
Cargo.lock generated

@ -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"

@ -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" }

@ -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);
}

@ -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<u64, String>
{
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() }
}
}

@ -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 }

@ -0,0 +1,8 @@
fn main()
{
let name = env!("CARGO_PKG_NAME");
println!("{} build.rs started...", name);
println!("{} build.rs finished...", name);
}

@ -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<i32>,
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<u64, String>
{
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);
}

@ -0,0 +1,2 @@
pub mod day_1;

@ -0,0 +1,10 @@
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82

@ -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()
}
Loading…
Cancel
Save