master
mitchellhansen 4 years ago
parent 9a4a516782
commit d53d13b872

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

@ -1 +1 @@
pub mod part1;
pub mod lib;

@ -1 +1 @@
pub mod part2;
pub mod lib;

@ -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<Vec<i32>>,
}
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) {
}
}

@ -0,0 +1 @@
pub mod lib;
Loading…
Cancel
Save