this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at 57a4e799a895935b0fbb5eea8653e43eefd49975 42 lines 1.1 kB view raw
1fn main() { 2 let input = std::fs::read_to_string("input.txt").unwrap(); 3 let trees: Vec<Vec<bool>> = input.trim().split("\n").map(line_to_trees).collect(); 4 5 println!("Part 1: {}", count_for_slope(trees.clone(), (3, 1))); 6 7 let slopes = vec![(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]; 8 9 let mut total = 1; 10 for slope in slopes { 11 total *= count_for_slope(trees.clone(), slope); 12 } 13 14 println!("Part 2: {}", total); 15} 16 17fn line_to_trees(line: &str) -> Vec<bool> { 18 line.chars().map(|c| c == '#').collect() 19} 20 21fn count_for_slope(trees: Vec<Vec<bool>>, slope: (usize, usize)) -> usize { 22 let mut pos = (0, 0); 23 let mut count = 0; 24 25 let row_length = trees.clone().first().unwrap().iter().count(); 26 let row_count = trees.clone().iter().count(); 27 28 while pos.1 < row_count { 29 let row = trees.iter().clone().nth(pos.1).unwrap(); 30 31 let x = pos.0 % row_length; 32 match row.iter().nth(x) { 33 Some(_) => if row[x] { count += 1; }, 34 None => panic!("Woops!"), 35 } 36 37 pos.0 += slope.0; 38 pos.1 += slope.1; 39 } 40 41 count 42}