Advertisement
Guest User

Advent of Code 2022 Day 8

a guest
Jan 13th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.48 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "io/ioutil"
  5.     "strconv"
  6.     "strings"
  7. )
  8.  
  9. func main() {
  10.     input, _ := ioutil.ReadFile("day_eight/input.txt")
  11.     trees := strings.Fields(strings.TrimSpace(string(input)))
  12.     println(calculateVisibleTrees(trees))
  13.     println(calcBestScenicScore(trees))
  14. }
  15.  
  16. func calculateVisibleTrees(trees []string) int {
  17.     visibleTrees := map[string]struct{}{}
  18.  
  19.     for row := 1; row < len(trees)-1; row++ {
  20.  
  21.         // find tallest from left to right
  22.         tallestTree, _ := strconv.Atoi(string(trees[row][0]))
  23.         for col := 1; col < len(trees[row])-1; col++ {
  24.             currentTree, _ := strconv.Atoi(string(trees[row][col]))
  25.             if currentTree > tallestTree {
  26.                 tallestTree = currentTree
  27.                 visibleTrees[strconv.Itoa(row)+" "+strconv.Itoa(col)] = struct{}{}
  28.             }
  29.         }
  30.  
  31.         // find tallest from right to left
  32.         tallestTree, _ = strconv.Atoi(string(trees[row][len(trees[row])-1]))
  33.         for col := len(trees[row]) - 2; col > 0; col-- {
  34.             currentTree, _ := strconv.Atoi(string(trees[row][col]))
  35.             if currentTree > tallestTree {
  36.                 tallestTree = currentTree
  37.                 visibleTrees[strconv.Itoa(row)+" "+strconv.Itoa(col)] = struct{}{}
  38.             }
  39.         }
  40.     }
  41.  
  42.     for col := 1; col < len(trees)-1; col++ {
  43.         // find tallest from top to bottom
  44.         tallestTree, _ := strconv.Atoi(string(trees[0][col]))
  45.         for row := 1; row < len(trees)-1; row++ {
  46.             currentTree, _ := strconv.Atoi(string(trees[row][col]))
  47.             if currentTree > tallestTree {
  48.                 tallestTree = currentTree
  49.                 visibleTrees[strconv.Itoa(row)+" "+strconv.Itoa(col)] = struct{}{}
  50.             }
  51.         }
  52.  
  53.         // find tallest from bottom to top
  54.         tallestTree, _ = strconv.Atoi(string(trees[len(trees)-1][col]))
  55.         for row := len(trees) - 2; row > 0; row-- {
  56.             currentTree, _ := strconv.Atoi(string(trees[row][col]))
  57.             if currentTree > tallestTree {
  58.                 tallestTree = currentTree
  59.                 visibleTrees[strconv.Itoa(row)+" "+strconv.Itoa(col)] = struct{}{}
  60.             }
  61.         }
  62.     }
  63.  
  64.     return (len(trees) * 2) + (len(trees[0]) * 2) + len(visibleTrees) - 4
  65. }
  66.  
  67. func calcBestScenicScore(trees []string) int {
  68.     bestScore := 0
  69.     for row := 0; row < len(trees)-1; row++ {
  70.         for col := 0; col < len(trees[row])-1; col++ {
  71.             currentScore := calcScenicScore(trees, row, col)
  72.             if currentScore > bestScore {
  73.                 bestScore = currentScore
  74.             }
  75.         }
  76.     }
  77.  
  78.     return bestScore
  79. }
  80.  
  81. func calcScenicScore(trees []string, row int, col int) int {
  82.     treeHeight, _ := strconv.Atoi(string(trees[row][col]))
  83.  
  84.     // check top
  85.     topScore := 0
  86.     for r := row - 1; ; r-- {
  87.         if r < 0 {
  88.             break
  89.         }
  90.  
  91.         currentTree, _ := strconv.Atoi(string(trees[r][col]))
  92.         if currentTree <= treeHeight {
  93.             topScore++
  94.         }
  95.  
  96.         if currentTree >= treeHeight {
  97.             break
  98.         }
  99.     }
  100.  
  101.     // check bottom
  102.     bottomScore := 0
  103.     for r := row + 1; ; r++ {
  104.         if r == len(trees) {
  105.             break
  106.         }
  107.  
  108.         currentTree, _ := strconv.Atoi(string(trees[r][col]))
  109.         if currentTree <= treeHeight {
  110.             bottomScore++
  111.         }
  112.  
  113.         if currentTree >= treeHeight {
  114.             break
  115.         }
  116.     }
  117.  
  118.     // check right
  119.     rightScore := 0
  120.     for c := col + 1; ; c++ {
  121.         if c == len(trees[row]) {
  122.             break
  123.         }
  124.  
  125.         currentTree, _ := strconv.Atoi(string(trees[row][c]))
  126.         if currentTree <= treeHeight {
  127.             rightScore++
  128.         }
  129.  
  130.         if currentTree >= treeHeight {
  131.             break
  132.         }
  133.     }
  134.  
  135.     // check left
  136.     leftScore := 0
  137.     for c := col - 1; ; c-- {
  138.         if c < 0 {
  139.             break
  140.         }
  141.  
  142.         currentTree, _ := strconv.Atoi(string(trees[row][c]))
  143.         if currentTree <= treeHeight {
  144.             leftScore++
  145.         }
  146.  
  147.         if currentTree >= treeHeight {
  148.             break
  149.         }
  150.     }
  151.  
  152.     return topScore * bottomScore * rightScore * leftScore
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement