This repository has been archived on 2024-05-09. You can view files and clone it, but cannot push or open issues/pull-requests.
advent/2020/day_3/src/main.rs

38 lines
1.1 KiB
Rust

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);
}