Advertisement
astrosofista

AoC8b 2022

Dec 8th, 2022
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Autohotkey 1.48 KB | Source Code | 0 0
  1. ProcessFile(fileName) {
  2.     FileRead, data, % fileName
  3.  
  4.     map := []
  5.  
  6.     For k, v in StrSplit(data, "`n") {
  7.         if (v = "")
  8.             continue
  9.         map.push(StrSplit(v))
  10.     }
  11.  
  12.     return map
  13. }
  14.  
  15. CountVisibleToTop(cell, map, x, y) {
  16.     visibleTrees := 0
  17.     while (y > 1) {
  18.         y--
  19.         visibleTrees++
  20.         if (cell <= map[y][x])
  21.             break
  22.     }
  23.     return visibleTrees
  24. }
  25.  
  26. CountVisibleToBottom(cell, map, x, y, maxY) {
  27.     visibleTrees := 0
  28.     while (y < maxY) {
  29.         y++
  30.         visibleTrees++
  31.         if (cell <= map[y][x])
  32.             break
  33.     }
  34.     return visibleTrees
  35. }
  36.  
  37. CountVisibleToLeft(cell, map, x, y) {
  38.     visibleTrees := 0
  39.     while (x > 1) {
  40.         x--
  41.         visibleTrees++
  42.         if (cell <= map[y][x])
  43.             break
  44.     }
  45.     return visibleTrees
  46. }
  47.  
  48. CountVisibleToRight(cell, map, x, y, maxX) {
  49.     visibleTrees := 0
  50.     while (x < maxX) {
  51.         x++
  52.         visibleTrees++
  53.         if (cell <= map[y][x])
  54.             break
  55.     }
  56.     return visibleTrees
  57. }
  58.  
  59. CalculateScenicScore(cell, map, x, y, maxX, maxY) {
  60.     return CountVisibleToTop(   cell, map, x, y)
  61.          * CountVisibleToBottom(cell, map, x, y, maxY)
  62.          * CountVisibleToLeft(  cell, map, x, y)
  63.          * CountVisibleToRight( cell, map, x, y, maxX)
  64. }
  65.  
  66. ProcessData(data) {
  67.     maxScenicScore := 0
  68.     maxY := data.count()
  69.     maxX := data[1].count()
  70.     y := 2
  71.     while (y < maxY) {
  72.         x := 2
  73.         while (x < maxX) {
  74.             cell := data[y][x]
  75.             maxScenicScore := max(maxScenicScore, CalculateScenicScore(cell, data, x, y, maxX, maxY))
  76.             x++
  77.         }
  78.         y++
  79.     }
  80.  
  81.     return maxScenicScore
  82. }
  83.  
  84. msgbox, % ProcessData(ProcessFile(A_ScriptDir "\aoc8.txt"))
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement