From d53d13b8726783836dc856852c33c222edd906fa Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Wed, 2 Dec 2020 22:39:30 -0800 Subject: [PATCH] problem 3 --- src/main.rs | 19 +++++--- src/problem1/{part1.rs => lib.rs} | 0 src/problem1/mod.rs | 2 +- src/problem2/{part2.rs => lib.rs} | 0 src/problem2/mod.rs | 2 +- src/problem3/lib.rs | 75 +++++++++++++++++++++++++++++++ src/problem3/mod.rs | 1 + 7 files changed, 91 insertions(+), 8 deletions(-) rename src/problem1/{part1.rs => lib.rs} (100%) rename src/problem2/{part2.rs => lib.rs} (100%) create mode 100644 src/problem3/lib.rs create mode 100644 src/problem3/mod.rs diff --git a/src/main.rs b/src/main.rs index 7abe676..e738cf1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,13 @@ extern crate reqwest; extern crate tempfile; -use crate::problem1::part1::Problem1; -use crate::problem2::part2::Problem2; +use crate::problem1::lib::Problem1; +use crate::problem2::lib::Problem2; +use crate::problem3::lib::Problem3; mod problem1; mod problem2; +mod problem3; mod util; @@ -23,7 +25,12 @@ fn main() { // problem1.run_part1(); // problem1.run_part2(); - let problem2 = Problem2::new(&util::get_problem(2)); - problem2.run_part1(); - problem2.run_part2(); -} \ No newline at end of file + //let problem2 = Problem2::new(&util::get_problem(2)); + //problem2.run_part1(); + //problem2.run_part2(); + + let problem3 = Problem3::new(&util::get_problem(3)); + problem3.run_part1(); + problem3.run_part2(); + +} diff --git a/src/problem1/part1.rs b/src/problem1/lib.rs similarity index 100% rename from src/problem1/part1.rs rename to src/problem1/lib.rs diff --git a/src/problem1/mod.rs b/src/problem1/mod.rs index e44fa95..965f28e 100644 --- a/src/problem1/mod.rs +++ b/src/problem1/mod.rs @@ -1 +1 @@ -pub mod part1; \ No newline at end of file +pub mod lib; diff --git a/src/problem2/part2.rs b/src/problem2/lib.rs similarity index 100% rename from src/problem2/part2.rs rename to src/problem2/lib.rs diff --git a/src/problem2/mod.rs b/src/problem2/mod.rs index c281610..965f28e 100644 --- a/src/problem2/mod.rs +++ b/src/problem2/mod.rs @@ -1 +1 @@ -pub mod part2; \ No newline at end of file +pub mod lib; diff --git a/src/problem3/lib.rs b/src/problem3/lib.rs new file mode 100644 index 0000000..4f343bc --- /dev/null +++ b/src/problem3/lib.rs @@ -0,0 +1,75 @@ +/* +Starting at the top-left corner of your map and following a slope of right 3 and down 1, how many trees would you encounter? +*/ + +use crate::Problem; + +pub struct Problem3 { + map: Vec>, +} + +impl Problem3 { + fn get_tile(&self, x: i32, y: i32) -> i32 { + let dim_y = self.map.len() as i32; + let dim_x = self.map.get(0).unwrap().len() as i32; + + let ind_x = (x % dim_x) as usize; + let ind_y = (y % dim_y) as usize; + + *self.map.get(ind_y).unwrap().get(ind_x).unwrap() + } + + fn traverse(&self, dx: i32, dy: i32) -> i64 { + let mut x = 0; + + let mut count = 0; + + for y in ((dy as usize)..self.map.len()).step_by(dy as usize) { + x += dx; + if self.get_tile(x as i32, y as i32) == 1 { + count += 1; +// println!("tree at {}, {}", x, y); + } + } + count as i64 + } +} + +impl Problem for Problem3 { + fn new(input: &String) -> Self { + Problem3 { + map: input + .split("\n") + .filter_map(|s| { + let s = s.trim(); + if !s.is_empty() { + let mut accum = Vec::new(); + for i in s.chars() { + if i == '#' { + accum.push(1); + } else { + accum.push(0); + } + } + Some(accum) + } else { + None + } + }).collect(), + } + } + + fn run_part1(&self) { + + let v : i64 = self.traverse(1, 1) * + self.traverse(3, 1) * + self.traverse(5, 1) * + self.traverse(7, 1) * + self.traverse(1, 2); + println!("{}", v); + } + + fn run_part2(&self) { + } +} + diff --git a/src/problem3/mod.rs b/src/problem3/mod.rs new file mode 100644 index 0000000..965f28e --- /dev/null +++ b/src/problem3/mod.rs @@ -0,0 +1 @@ +pub mod lib;