JonathanGupton

Advent of Code 2022 - Day 8

Dec 8th, 2022 (edited)
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.94 KB | None | 0 0
  1. #[derive(Debug, Copy, Clone, PartialEq)]
  2. enum Visibility {
  3.     Visible,
  4.     Invisible,
  5. }
  6.  
  7. type VisibilityGrid = Vec<Vec<Visibility>>;
  8. type TreeGrid = Vec<Vec<u32>>;
  9.  
  10. fn make_tree_grid(input: &str) -> TreeGrid {
  11.     input
  12.         .lines()
  13.         .map(|l| l.chars().map(|c| c.to_digit(10).unwrap()).collect())
  14.         .collect()
  15. }
  16.  
  17. fn make_tree_visibility_grid(tree_grid: &Vec<Vec<u32>>) -> VisibilityGrid {
  18.     let length = tree_grid.len();
  19.     let width = tree_grid.get(0).unwrap().len();
  20.     let mut visibility_grid: Vec<Vec<Visibility>> = Vec::new();
  21.     for _ in 0..length {
  22.         visibility_grid.push(vec![Visibility::Invisible; width]);
  23.     }
  24.     visibility_grid
  25. }
  26.  
  27. fn assess_tree_visibility(
  28.     mut visibility_grid: VisibilityGrid,
  29.     tree_grid: &TreeGrid,
  30. ) -> VisibilityGrid {
  31.     let length = tree_grid.len();
  32.     let width = tree_grid[0].len();
  33.     let mut local_max: u32;
  34.  
  35.     // assess left to right
  36.     for i in 0..length {
  37.         local_max = tree_grid[i][0];
  38.         visibility_grid[i][0] = Visibility::Visible;
  39.         for j in 1..width {
  40.             if tree_grid[i][j] > local_max {
  41.                 local_max = tree_grid[i][j];
  42.                 visibility_grid[i][j] = Visibility::Visible;
  43.             }
  44.         }
  45.     }
  46.     // assess right to left
  47.     for i in 0..length {
  48.         local_max = tree_grid[i][width - 1];
  49.         visibility_grid[i][length - 1] = Visibility::Visible;
  50.         for j in (0..width).rev() {
  51.             if tree_grid[i][j] > local_max {
  52.                 local_max = tree_grid[i][j];
  53.                 visibility_grid[i][j] = Visibility::Visible;
  54.             }
  55.         }
  56.     }
  57.     // assess top to bottom
  58.     for i in 0..width {
  59.         local_max = tree_grid[0][i];
  60.         visibility_grid[0][i] = Visibility::Visible;
  61.         for j in 0..length {
  62.             if tree_grid[j][i] > local_max {
  63.                 local_max = tree_grid[j][i];
  64.                 visibility_grid[j][i] = Visibility::Visible;
  65.             }
  66.         }
  67.     }
  68.     // assess bottom to top
  69.     for i in 0..width {
  70.         local_max = tree_grid[length - 1][i];
  71.         visibility_grid[length - 1][i] = Visibility::Visible;
  72.         for j in (0..length).rev() {
  73.             if tree_grid[j][i] > local_max {
  74.                 local_max = tree_grid[j][i];
  75.                 visibility_grid[j][i] = Visibility::Visible;
  76.             }
  77.         }
  78.     }
  79.     visibility_grid
  80. }
  81.  
  82. fn assess_scenic_score(tree_grid: TreeGrid) -> u32 {
  83.     let mut scenic_score: u32 = 0;
  84.     let length = tree_grid.len();
  85.     let width = tree_grid[0].len();
  86.     let mut up: u32;
  87.     let mut left: u32;
  88.     let mut right: u32;
  89.     let mut down: u32;
  90.     for i in 1..length - 1 {
  91.         for j in 1..width - 1 {
  92.             let tree_height = tree_grid[i][j];
  93.  
  94.             // assess up
  95.             up = 0;
  96.             for n in (0..i).rev() {
  97.                 up += 1;
  98.                 if tree_grid[n][j] < tree_height {
  99.                     continue;
  100.                 } else {
  101.                     break;
  102.                 }
  103.             }
  104.  
  105.             // assess left
  106.             left = 0;
  107.             for n in (0..j).rev() {
  108.                 left += 1;
  109.                 if tree_grid[i][n] < tree_height {
  110.                     continue;
  111.                 } else {
  112.                     break;
  113.                 }
  114.             }
  115.  
  116.             // assess right
  117.             right = 0;
  118.             for n in j + 1..width {
  119.                 right += 1;
  120.                 if tree_grid[i][n] < tree_height {
  121.                     continue;
  122.                 } else {
  123.                     break;
  124.                 }
  125.             }
  126.  
  127.             // assess down
  128.             down = 0;
  129.             for n in i + 1..length {
  130.                 down += 1;
  131.                 if tree_grid[n][j] < tree_height {
  132.                     continue;
  133.                 } else {
  134.                     break;
  135.                 }
  136.             }
  137.  
  138.             // check scenic score
  139.             if up * right * down * left > scenic_score {
  140.                 scenic_score = up * right * down * left;
  141.             }
  142.         }
  143.     }
  144.  
  145.     scenic_score
  146. }
  147.  
  148. pub fn part_one(input: &str) -> Option<u32> {
  149.     let tree_grid = make_tree_grid(input);
  150.     let mut visibility_grid = make_tree_visibility_grid(&tree_grid);
  151.     visibility_grid = assess_tree_visibility(visibility_grid, &tree_grid);
  152.     let length = tree_grid.len();
  153.     let width = tree_grid[0].len();
  154.     let mut visible_count = 0;
  155.     for i in 0..length {
  156.         for j in 0..width {
  157.             if visibility_grid[i][j] == Visibility::Visible {
  158.                 visible_count += 1;
  159.             }
  160.         }
  161.     }
  162.     Some(visible_count)
  163. }
  164.  
  165. pub fn part_two(input: &str) -> Option<u32> {
  166.     let tree_grid = make_tree_grid(input);
  167.     Some(assess_scenic_score(tree_grid))
  168. }
  169.  
  170. fn main() {
  171.     let input = &advent_of_code::read_file("inputs", 8);
  172.     advent_of_code::solve!(1, part_one, input); //
  173.     advent_of_code::solve!(2, part_two, input); //
  174. }
Advertisement
Add Comment
Please, Sign In to add comment