1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
fn ss(r: Vec<u32>, t: u32) -> u32 {
let mut i: usize = 0;
for x in r {
if x >= t {
i+=1; // If we're blocked, count the blocking tree as visible!
break;
}
i+=1;
}
i.try_into().unwrap()
}
// fn visible(r: Vec<u32>, t: u32) -> bool {
// r.into_iter().all(|tt| { tt < t })
// }
pub fn run(input: String) {
let grid: Vec<Vec<u32>> = input.lines().map(|line| {
line.chars().map(|ch| {
ch.to_digit(10).expect("char wasn't a digit!")
}).collect()
}).collect();
let height = grid.len();
let width = grid[0].len();
// Naïve solution, just check every tree's path's to the edge
// let visibilities: Vec<Vec<bool>> =
let viewingscores: Vec<Vec<u32>> =
(0..height).map(|i| {
(0..width).map(|j| {
if i == 0 || j == 0 || i == height-1 || j == width-1 {
// println!("short circuit {} {}",i, j);
//return true;
return 0;
}
let t = grid[i][j];
let r1: Vec<u32> = (0..i).rev().map(|ii| {grid[ii][j]} ).collect();
let r2: Vec<u32> = (i+1..height).map(|ii| {grid[ii][j]} ).collect();
let r3: Vec<u32> = (0..j).rev().map(|jj| {grid[i][jj]} ).collect();
let r4: Vec<u32> = (j+1..width).map(|jj| {grid[i][jj]}).collect();
// let visible = [r1, r2, r3, r4].into_iter().any(|r| {
// visible(r, t)
// });
// visible
// Always see at least 1 tree in any direction
[r1, r2, r3, r4].into_iter().map(|r| { ss(r, t) } ).product()
}).collect()
}).collect();
// println!("{:?}", grid);
// println!("{:?}", visibilities);
// let nvisible: u32 = visibilities.into_iter().flatten().into_iter().map(|b| {if b {1} else {0}}).sum();
// println!("Day 8: {}", nvisible);
let maxvs = viewingscores.into_iter().flatten().max().unwrap();
println!("Day 8: {}", maxvs);
}
|