From 12ca48593a4383593727c269f59f717afb6a87da Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Fri, 9 Dec 2022 10:58:08 +0000 Subject: day8 --- src/day8.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/day8.rs (limited to 'src/day8.rs') diff --git a/src/day8.rs b/src/day8.rs new file mode 100644 index 0000000..e69d5cd --- /dev/null +++ b/src/day8.rs @@ -0,0 +1,56 @@ +fn ss(r: Vec, 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, t: u32) -> bool { +// r.into_iter().all(|tt| { tt < t }) +// } + +pub fn run(input: String) { + let grid: Vec> = 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> = + let viewingscores: Vec> = + (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 = (0..i).rev().map(|ii| {grid[ii][j]} ).collect(); + let r2: Vec = (i+1..height).map(|ii| {grid[ii][j]} ).collect(); + let r3: Vec = (0..j).rev().map(|jj| {grid[i][jj]} ).collect(); + let r4: Vec = (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); +} \ No newline at end of file -- cgit v1.2.3-ZIG