-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.go
149 lines (109 loc) Β· 2.92 KB
/
day08.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package day08
import (
"strconv"
"strings"
)
type Point struct {
x int
y int
}
type TreeMap struct {
grid [][]int
width int
height int
}
func parseTreeMap(input string) TreeMap {
var grid [][]int
for _, treeRow := range strings.Split(input, "\n") {
var parsedRow []int
for _, tree := range strings.Split(treeRow, "") {
treeHeight, err := strconv.Atoi(tree)
if err != nil {
panic("Error parsing tree map")
}
parsedRow = append(parsedRow, treeHeight)
}
grid = append(grid, parsedRow)
}
height := len(grid)
width := len(grid[0])
return TreeMap{grid, width, height}
}
func (treeMap *TreeMap) isVisibleTop(point Point) (bool, int) {
treeHeight := treeMap.grid[point.y][point.x]
treesInView := 0
for y := point.y - 1; y >= 0; y-- {
treesInView++
if treeMap.grid[y][point.x] >= treeHeight {
return false, treesInView
}
}
return true, treesInView
}
func (treeMap *TreeMap) isVisibleBottom(point Point) (bool, int) {
treeHeight := treeMap.grid[point.y][point.x]
treesInView := 0
for y := point.y + 1; y < treeMap.height; y++ {
treesInView++
if treeMap.grid[y][point.x] >= treeHeight {
return false, treesInView
}
}
return true, treesInView
}
func (treeMap *TreeMap) isVisibleLeft(point Point) (bool, int) {
treeHeight := treeMap.grid[point.y][point.x]
treesInView := 0
for x := point.x - 1; x >= 0; x-- {
treesInView++
if treeMap.grid[point.y][x] >= treeHeight {
return false, treesInView
}
}
return true, treesInView
}
func (treeMap *TreeMap) isVisibleRight(point Point) (bool, int) {
treeHeight := treeMap.grid[point.y][point.x]
treesInView := 0
for x := point.x + 1; x < treeMap.width; x++ {
treesInView++
if treeMap.grid[point.y][x] >= treeHeight {
return false, treesInView
}
}
return true, treesInView
}
func part1(input string) (visibleTrees int) {
treeMap := parseTreeMap(input)
visibleTrees = (treeMap.width * 2) + (treeMap.height * 2) - 4
for y := 1; y < treeMap.height-1; y++ {
for x := 1; x < treeMap.width-1; x++ {
point := Point{x, y}
topVisible, _ := treeMap.isVisibleTop(point)
bottomVisible, _ := treeMap.isVisibleBottom(point)
leftVisible, _ := treeMap.isVisibleLeft(point)
rightVisible, _ := treeMap.isVisibleRight(point)
if topVisible || bottomVisible || leftVisible || rightVisible {
visibleTrees++
}
}
}
return
}
func part2(input string) (highestScenicScore int) {
treeMap := parseTreeMap(input)
for y := 1; y < treeMap.height-1; y++ {
for x := 1; x < treeMap.width-1; x++ {
point := Point{x, y}
_, topTreesInView := treeMap.isVisibleTop(point)
_, bottomTreesInView := treeMap.isVisibleBottom(point)
_, leftTreesInView := treeMap.isVisibleLeft(point)
_, rightTreesInView := treeMap.isVisibleRight(point)
scenicScore := topTreesInView * bottomTreesInView * leftTreesInView * rightTreesInView
if scenicScore > highestScenicScore {
highestScenicScore = scenicScore
}
}
}
return
}