Compare commits

..

No commits in common. 'da6f15140252ff6619f0a52b664b4adba99428b6' and 'c1c963c7fbea06b0b2fc1031272c927b8f791ab8' have entirely different histories.

@ -7,20 +7,15 @@
* @brief pre-advent testing * @brief pre-advent testing
******************************************************************************/ ******************************************************************************/
// use std::path::absolute;
use solver::solver::{Solver, SolverOption, SolverState}; use solver::solver::{Solver, SolverOption, SolverState};
use utils::utils; use utils::utils;
const DIAL_MAX_POS: i32 = 99;
pub struct Day1 pub struct Day1
{ {
solver_state: SolverState, solver_state: SolverState,
input: String, input: String,
dial_position: i32, dial_position: i32,
rotations: Vec<i32>, rotations: Vec<i32>,
// full_rots: Vec<i32>,
zeros: u64, zeros: u64,
} }
@ -29,86 +24,34 @@ impl Solver for Day1
fn init(&mut self, config: solver::solver::SolverState) -> Result<(), String> fn init(&mut self, config: solver::solver::SolverState) -> Result<(), String>
{ {
self.solver_state = config; self.solver_state = config;
if self.solver_state.check_option(SolverOption::Debug)
{
self.input = utils::read_text_file("input/day_1_test_input"); 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(); self.parse_rots();
return Ok(()) return Ok(())
} }
fn solve(&mut self, _input: String) -> Result<u64, String> fn solve(&mut self, _input: String) -> Result<u64, String>
{ {
// 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!("Starting Dial Pos: {}", self.dial_position);
println!("Number of rotations: {}", self.rotations.len());
for rot in &self.rotations for rot in &self.rotations
{ {
let (new_pos, num_zeros) = Day1::process_rot(self.dial_position, *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;
// } if verbose
// else if self.dial_position > DIAL_MAX_POS {
// { println!("Applied rotation {}, dial pos: {}", rot, self.dial_position);
// 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 if 0 == self.dial_position
// { {
// self.zeros += 1; self.zeros += 1;
// if verbose if verbose
// { {
// println!("Zero found, total: {}", self.zeros); println!("Zero found, total: {}", self.zeros);
// } }
// } }
} }
Ok(self.zeros) Ok(0)
} }
fn name(&self) -> String fn name(&self) -> String
@ -121,8 +64,7 @@ impl Day1
{ {
pub fn new() -> Day1 pub fn new() -> Day1
{ {
Day1 { solver_state: SolverState::new(), input: "".to_string(), dial_position: 50, Day1 { solver_state: SolverState::new(), input: "".to_string(), dial_position: 50, rotations: vec![], zeros: 0 }
rotations: vec![], zeros: 0 }
} }
fn parse_rots(&mut self) fn parse_rots(&mut self)
@ -140,61 +82,15 @@ impl Day1
let trimmed = &line[1..]; let trimmed = &line[1..];
let 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
// {
// 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; let rot = mag * coef;
self.rotations.push(rot); self.rotations.push(rot);
// println!("as: {}", rot); // println!("as: {}", rot);
} }
} }
/// Returns the new dial_position and number of zeros landed on during this rotation // fn process_rot(&mut self, )
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) fn print_rot(r: i32)
{ {
let mut rot = r; let mut rot = r;

@ -65,11 +65,6 @@ impl SolverState
pub fn get_value(self: &SolverState, key: &str) -> String pub fn get_value(self: &SolverState, key: &str) -> String
{ {
if !self.values.contains_key(key)
{
return "".to_string();
}
self.values[&key.to_string()].clone() self.values[&key.to_string()].clone()
} }
} }

File diff suppressed because it is too large Load Diff

@ -15,11 +15,10 @@ fn main()
// TODO: Parse args into a vec of SolverStates, one for each day // 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 // 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(); run_day_1();
} }
#[allow(dead_code)]
fn run_test_day() fn run_test_day()
{ {
let mut state = SolverState::new(); let mut state = SolverState::new();
@ -40,11 +39,7 @@ fn run_test_day()
fn run_day_1() 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_value("true", "count_passing_zeros");
let mut day1 = Day1::new(); let mut day1 = Day1::new();
day1.init(state).expect(&format!("Day {} failed to init", day1.name())); day1.init(state).expect(&format!("Day {} failed to init", day1.name()));
@ -62,7 +57,6 @@ fn run_day_1()
/// and `-g debug` which is: global option: "debug". Global options will be added to each day that is run. /// 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. /// 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. /// `-r all` will run all days.
#[allow(dead_code)]
fn parse_args() fn parse_args()
{ {
// TODO: implement parse_args() // TODO: implement parse_args()

Loading…
Cancel
Save