adventofcode

git clone https://git.ce9e.org/adventofcode.git

commit
9d4a564a8f9da8d47a025c080021032fd06fad74
parent
848add314a357f6eccdbec2a37223c603f8f324c
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2022-12-08 08:55
refactor

Diffstat

M 2022/08/solution.rs 129 ++++++++++++++++++++----------------------------------------

1 files changed, 43 insertions, 86 deletions


diff --git a/2022/08/solution.rs b/2022/08/solution.rs

@@ -8,19 +8,18 @@ fn get_map() -> Vec<Vec<u8>> {
    8     8     return map;
    9     9 }
   10    10 
   11    -1 fn part1(heights: &Vec<Vec<u8>>) -> usize {
   12    -1     let rows = heights.len();
   13    -1     let cols = heights[0].len();
   14    -1     assert_eq!(rows, cols);
   15    -1 
   16    -1     let mut visible: Vec<Vec<bool>> = heights.iter().map(|row|
   17    -1         row.iter().map(|_| false).collect()
   18    -1     ).collect();
   -1    11 fn mark_visible(
   -1    12     heights: &Vec<Vec<u8>>,
   -1    13     visible: &mut Vec<Vec<bool>>,
   -1    14     vertical: bool,
   -1    15     reverse: bool,
   -1    16 ) {
   19    17     let mut max;
   20    -1 
   21    -1     for x in 0..cols {
   -1    18     for a in 0..heights.len() {
   22    19         max = 0;
   23    -1         for y in 0..rows {
   -1    20         for b in 0..heights.len() {
   -1    21             let c = if reverse { heights.len() - (b + 1) } else { b };
   -1    22             let (y, x) = if vertical { (c, a) } else { (a, c) };
   24    23             if heights[y][x] > max {
   25    24                 max = heights[y][x];
   26    25                 visible[y][x] = true;
@@ -28,100 +27,58 @@ fn part1(heights: &Vec<Vec<u8>>) -> usize {
   28    27         }
   29    28     }
   30    29 
   31    -1     for x in 0..cols {
   32    -1         max = 0;
   33    -1         for y in (0..rows).rev() {
   34    -1             if heights[y][x] > max {
   35    -1                 max = heights[y][x];
   36    -1                 visible[y][x] = true;
   37    -1             }
   38    -1         }
   39    -1     }
   -1    30 }
   40    31 
   41    -1     for y in 0..rows {
   42    -1         max = 0;
   43    -1         for x in 0..cols {
   44    -1             if heights[y][x] > max {
   45    -1                 max = heights[y][x];
   46    -1                 visible[y][x] = true;
   47    -1             }
   48    -1         }
   49    -1     }
   -1    32 fn part1(heights: &Vec<Vec<u8>>) -> usize {
   -1    33     let mut visible: Vec<Vec<bool>> = heights.iter().map(|row|
   -1    34         row.iter().map(|_| false).collect()
   -1    35     ).collect();
   50    36 
   51    -1     for y in 0..rows {
   52    -1         max = 0;
   53    -1         for x in (0..cols).rev() {
   54    -1             if heights[y][x] > max {
   55    -1                 max = heights[y][x];
   56    -1                 visible[y][x] = true;
   57    -1             }
   -1    37     mark_visible(heights, &mut visible, false, false);
   -1    38     mark_visible(heights, &mut visible, false, true);
   -1    39     mark_visible(heights, &mut visible, true, false);
   -1    40     mark_visible(heights, &mut visible, true, true);
   -1    41 
   -1    42     return visible.concat().iter().filter(|v| **v).count();
   -1    43 }
   -1    44 
   -1    45 fn count_visible(value: u8, others: impl Iterator<Item = u8>) -> u64 {
   -1    46     let mut count = 0;
   -1    47     for other in others {
   -1    48         count += 1;
   -1    49         if other >= value {
   -1    50             return count;
   58    51         }
   59    52     }
   60    -1 
   61    -1     return visible.iter().map(|row| row.iter().filter(|v| **v).count()).sum();
   -1    53     return count;
   62    54 }
   63    55 
   64    -1 fn part2(heights: &Vec<Vec<u8>>) -> usize {
   -1    56 fn part2(heights: &Vec<Vec<u8>>) -> u64 {
   65    57     let rows = heights.len();
   66    58     let cols = heights[0].len();
   67    -1     assert_eq!(rows, cols);
   68    -1 
   69    -1     let mut count;
   70    -1     let mut score;
   71    -1 
   72    -1     let mut scores: Vec<Vec<usize>> = heights.iter().map(|row|
   73    -1         row.iter().map(|_| 0).collect()
   74    -1     ).collect();
   -1    59     let mut max = 0;
   75    60 
   76    61     for x in 0..cols {
   77    62         for y in 0..rows {
   78    -1             score = 1;
   79    -1 
   80    -1             count = 0;
   81    -1             for x1 in (0..x).rev() {
   82    -1                 count += 1;
   83    -1                 if heights[y][x1] >= heights[y][x] {
   84    -1                     break;
   85    -1                 }
   86    -1             }
   87    -1             score *= count;
   88    -1 
   89    -1             count = 0;
   90    -1             for x1 in (x + 1)..cols {
   91    -1                 count += 1;
   92    -1                 if heights[y][x1] >= heights[y][x] {
   93    -1                     break;
   94    -1                 }
   -1    63             let value = heights[y][x];
   -1    64             let score = count_visible(value, (0..x).rev().map(|x1| heights[y][x1]))
   -1    65                 * count_visible(value, (x + 1..cols).map(|x1| heights[y][x1]))
   -1    66                 * count_visible(value, (0..y).rev().map(|y1| heights[y1][x]))
   -1    67                 * count_visible(value, (y + 1..rows).map(|y1| heights[y1][x]));
   -1    68 
   -1    69             if score > max {
   -1    70                 max = score;
   95    71             }
   96    -1             score *= count;
   97    -1 
   98    -1             count = 0;
   99    -1             for y1 in (0..y).rev() {
  100    -1                 count += 1;
  101    -1                 if heights[y1][x] >= heights[y][x] {
  102    -1                     break;
  103    -1                 }
  104    -1             }
  105    -1             score *= count;
  106    -1 
  107    -1             count = 0;
  108    -1             for y1 in (y + 1)..rows {
  109    -1                 count += 1;
  110    -1                 if heights[y1][x] >= heights[y][x] {
  111    -1                     break;
  112    -1                 }
  113    -1             }
  114    -1             score *= count;
  115    -1 
  116    -1             scores[y][x] = score;
  117    72         }
  118    73     }
  119    74 
  120    -1     return scores.concat().iter().map(|x| *x).max().unwrap();
   -1    75     return max;
  121    76 }
  122    77 
  123    78 fn main() {
  124    79     let heights = get_map();
   -1    80     assert_eq!(heights.len(), heights[0].len());
   -1    81 
  125    82     println!("part1: {}", part1(&heights));
  126    83     println!("part2: {}", part2(&heights));
  127    84 }