day 3 solution

This commit is contained in:
2020-12-03 13:07:51 -05:00
parent 4a0f4584dd
commit 7321e315bd
4 changed files with 380 additions and 0 deletions

37
2020/day_3/src/main.rs Normal file
View File

@@ -0,0 +1,37 @@
fn main() {
let mut hill: Vec<String> = Vec::new();
let contents = std::fs::read_to_string("input1").unwrap();
for s in contents.lines() {
hill.push(s.parse::<String>().unwrap());
}
let rounds = [[1, 1], [3, 1], [5, 1], [7, 1], [1, 2]];
// let rounds = [[1, 2]];
let mut hit_trees = Vec::new();
for r in rounds.iter() {
let right_rule = r[0];
let down_rule = r[1];
let mut right_pos = 0;
let mut down_pos = 0;
let mut trees: i64 = 0;
loop {
for _ in 0..right_rule {
if right_pos == hill[down_pos].len() - 1 {
right_pos = 0;
} else {
right_pos += 1;
}
}
down_pos += down_rule;
if hill[down_pos].chars().nth(right_pos).unwrap() == '#' {
trees += 1;
}
if hill.len() - 1 == down_pos {
break;
}
}
hit_trees.push(trees);
}
let total = hit_trees[0] * hit_trees[1] * hit_trees[2] * hit_trees[3] * hit_trees[4];
print!("You hit {} total trees!\n", total);
}