Skip to content

Instantly share code, notes, and snippets.

@s3thi
Created December 4, 2020 08:14
Show Gist options
  • Save s3thi/672137eaf77b74846178c84e7446af64 to your computer and use it in GitHub Desktop.
Save s3thi/672137eaf77b74846178c84e7446af64 to your computer and use it in GitHub Desktop.
use std::collections::HashSet;
use std::fs::read_to_string;
fn main() {
let input = read_to_string("input.txt").unwrap();
let mut x = 0;
let mut max_x = 0;
let mut y = 0;
let mut trees: HashSet<(u16, u16)> = HashSet::new();
for c in input.chars() {
if c == '\n' {
y = y + 1;
max_x = x - 1;
x = 0;
continue;
}
if c == '#' {
trees.insert((x, y));
}
x = x + 1;
}
let max_y = y - 1;
let rt1_dn1 = count_trees(&trees, max_x, max_y, 1, 1);
let rt3_dn1 = count_trees(&trees, max_x, max_y, 3, 1);
let rt5_dn1 = count_trees(&trees, max_x, max_y, 5, 1);
let rt7_dn1 = count_trees(&trees, max_x, max_y, 7, 1);
let rt1_dn2 = count_trees(&trees, max_x, max_y, 1, 2);
println!("Tree count for RT1, DN1: {}", rt1_dn1);
println!("Tree count for RT3, DN1: {}", rt3_dn1);
println!("Tree count for RT5, DN1: {}", rt5_dn1);
println!("Tree count for RT7, DN1: {}", rt7_dn1);
println!("Tree count for RT1, DN2: {}", rt1_dn2);
let product: u64 = rt1_dn1 * rt3_dn1 * rt5_dn1 * rt7_dn1 * rt1_dn2;
println!("Product: {}", product);
}
fn count_trees(
trees: &HashSet<(u16, u16)>,
max_x: u16,
max_y: u16,
right_count: u16,
down_count: u16,
) -> u64 {
let mut tob_x = 0;
let mut tob_y = 0;
let mut tree_count = 0;
loop {
if trees.contains(&(tob_x, tob_y)) {
tree_count = tree_count + 1;
}
tob_x = (tob_x + right_count) % (max_x + 1);
tob_y = tob_y + down_count;
if tob_y > max_y {
break;
}
}
tree_count
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment