aoc2022

Advent of Code 2022 solutions in Rust
git clone git://code.mfashby.net:/aoc2022
Log | Files | Refs

day8.rs (2010B)


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