Day 1 Part 1 Working

master
Joey Pollack 1 week ago
parent c1c963c7fb
commit f6b77d6062

@ -10,6 +10,8 @@
use solver::solver::{Solver, SolverOption, SolverState};
use utils::utils;
const DIAL_MAX_POS: i32 = 99;
pub struct Day1
{
solver_state: SolverState,
@ -24,7 +26,14 @@ impl Solver for Day1
fn init(&mut self, config: solver::solver::SolverState) -> Result<(), String>
{
self.solver_state = config;
if self.solver_state.check_option(SolverOption::Debug)
{
self.input = utils::read_text_file("input/day_1_test_input");
}
else
{
self.input = utils::read_text_file("input/day_1_input");
}
self.parse_rots();
return Ok(())
}
@ -33,9 +42,18 @@ impl Solver for Day1
{
let verbose = self.solver_state.check_option(SolverOption::Verbose);
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
{
@ -51,7 +69,7 @@ impl Solver for Day1
}
}
}
Ok(0)
Ok(self.zeros)
}
fn name(&self) -> String
@ -81,7 +99,12 @@ impl Day1
// 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 mut 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
}
let rot = mag * coef;
self.rotations.push(rot);
// println!("as: {}", rot);
@ -91,6 +114,8 @@ impl Day1
// fn process_rot(&mut self, )
}
#[allow(dead_code)]
fn print_rot(r: i32)
{
let mut rot = r;

File diff suppressed because it is too large Load Diff

@ -15,10 +15,11 @@ fn main()
// 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_test_day();
run_day_1();
}
#[allow(dead_code)]
fn run_test_day()
{
let mut state = SolverState::new();
@ -39,7 +40,10 @@ fn run_test_day()
fn run_day_1()
{
println!("Running Day 1...");
let state = SolverState::new();
// state.set_option(SolverOption::Debug);
// state.set_option(SolverOption::Verbose);
let mut day1 = Day1::new();
day1.init(state).expect(&format!("Day {} failed to init", day1.name()));
@ -57,6 +61,7 @@ fn run_day_1()
/// 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.
#[allow(dead_code)]
fn parse_args()
{
// TODO: implement parse_args()

Loading…
Cancel
Save