AOC2022/day8/day8.go
2022-12-09 21:37:26 +01:00

85 lines
2.1 KiB
Go

package main
import (
"AOC2022/helper"
"fmt"
"os"
)
type Tree struct {
height int
scenicScone int
}
func main() {
args := os.Args[1:]
lines := helper.ReadTextFile(args[0])
forest := make([][]Tree, len(lines))
for i, line := range lines {
forest[i] = make([]Tree, len(line))
for j, tree := range line {
forest[i][j] = Tree{int(tree - 48), 1}
}
}
part1and2(forest)
}
func part1and2(forest [][]Tree) {
sum := 0
highestScenicScore := 0
width := len(forest[0])
for i := 1; i < len(forest)-1; i++ {
for j := 1; j < width-1; j++ {
if checkVisibleAllDir([2]int{i, j}, &forest) {
sum++
}
scenicScore := getScenicScore([2]int{i, j}, &forest)
if scenicScore > highestScenicScore {
highestScenicScore = scenicScore
}
}
}
fmt.Println(sum + len(forest)*2 + len(forest[0])*2 - 4)
fmt.Println(highestScenicScore)
}
func checkVisibleAllDir(startlocation [2]int, forest *[][]Tree) bool {
directions := [][2]int{{0, -1}, {0, +1}, {-1, 0}, {+1, 0}}
visible := false
for _, direction := range directions {
if checkVisibleDir(startlocation, direction, forest) {
visible = true
}
}
return visible
}
func checkVisibleDir(startlocation, direction [2]int, forest *[][]Tree) bool {
currentLocation := startlocation
scenicscore := 0
for currentLocation[0] > 0 && currentLocation[1] > 0 &&
currentLocation[0] < len(*forest)-1 && currentLocation[1] < len((*forest)[0])-1 {
currentLocation[0] += direction[0]
currentLocation[1] += direction[1]
scenicscore++
if getHeight(currentLocation, forest) >= getHeight(startlocation, forest) {
addScenicScore(startlocation, forest, scenicscore)
return false
}
}
addScenicScore(startlocation, forest, scenicscore)
return true
}
func getHeight(location [2]int, forest *[][]Tree) int {
return (*forest)[location[0]][location[1]].height
}
func getScenicScore(location [2]int, forest *[][]Tree) int {
return (*forest)[location[0]][location[1]].scenicScone
}
func addScenicScore(location [2]int, forest *[][]Tree, scenicScore int) {
(*forest)[location[0]][location[1]].scenicScone *= scenicScore
}