Compare commits

...

2 Commits

Author SHA1 Message Date
Joey Pollack da6f151402 Day 1 Finished 1 week ago
Joey Pollack f6b77d6062 Day 1 Part 1 Working 1 week ago

@ -7,15 +7,20 @@
* @brief pre-advent testing
******************************************************************************/
// use std::path::absolute;
use solver::solver::{Solver, SolverOption, SolverState};
use utils::utils;
const DIAL_MAX_POS: i32 = 99;
pub struct Day1
{
solver_state: SolverState,
input: String,
dial_position: i32,
rotations: Vec<i32>,
// full_rots: Vec<i32>,
zeros: u64,
}
@ -24,34 +29,86 @@ 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");
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(())
}
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!("Number of rotations: {}", self.rotations.len());
for rot in &self.rotations
{
self.dial_position += rot;
let (new_pos, num_zeros) = Day1::process_rot(self.dial_position, *rot);
self.dial_position = new_pos;
self.zeros += num_zeros;
if verbose
{
println!("Applied rotation {}, dial pos: {}", rot, self.dial_position);
}
// self.dial_position += rot;
// if verbose
// {
// println!("Applied rotation {}", rot);
// }
if 0 == self.dial_position
{
self.zeros += 1;
if verbose
{
println!("Zero found, total: {}", self.zeros);
}
}
// 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(0)
Ok(self.zeros)
}
fn name(&self) -> String
@ -64,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)
@ -82,15 +140,61 @@ impl Day1
let trimmed = &line[1..];
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;
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)
{
let mut rot = r;

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

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,11 @@ fn run_test_day()
fn run_day_1()
{
let state = SolverState::new();
println!("Running Day 1...");
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();
day1.init(state).expect(&format!("Day {} failed to init", day1.name()));
@ -57,6 +62,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