aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

b.rs (968B)


      1 use std::cmp::max;
      2 mod common;
      3 use common::*;
      4 
      5 fn count_visible_from(grid: &Grid, i0: usize, j0: usize) -> usize {
      6     let x = grid[i0][j0].0;
      7     let mut top = 0;
      8     for i in (0..i0).rev() {
      9         top += 1;
     10         if grid[i][j0].0 >= x { break; }
     11     }
     12 
     13     let mut bottom = 0;
     14     for i in i0+1..grid.len() {
     15         bottom += 1;
     16         if grid[i][j0].0 >= x { break; }
     17     }
     18 
     19     let mut left = 0;
     20     for j in (0..j0).rev() {
     21         left += 1;
     22         if grid[i0][j].0 >= x { break; }
     23     }
     24 
     25     let mut right = 0;
     26     for j in j0+1..grid[0].len() {
     27         right += 1;
     28         if grid[i0][j].0 >= x { break; }
     29     }
     30 
     31     top * bottom * left * right
     32 }
     33 
     34 fn max_view_factor(grid: &Grid) -> usize {
     35     let mut m = 0;
     36     for i in 0..grid.len() {
     37         for j in 0..grid[0].len() {
     38             m = max(m, count_visible_from(grid, i, j));
     39         }
     40     }
     41     m
     42 }
     43 
     44 fn main() {
     45     let grid = read_grid_from_stdin();
     46     println!("{}", max_view_factor(&grid));
     47 }