-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (74 loc) · 1.78 KB
/
main.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
package main
import (
"fmt"
utils "github.com/baspar/adventofcode2022/internal"
)
type Grid [][]int
func (g Grid) isOutOfBound(i, j int) bool {
return i < 0 || j < 0 || i >= len(g) || j >= len(g[i])
}
func (g Grid) getVisibilityDistance(i, j, di, dj int) int {
for n := 0; ; n++ {
if g.isOutOfBound(i+n*di, j+n*dj) {
return n - 1
}
if n != 0 && g[i+n*di][j+n*dj] >= g[i][j] {
return n
}
}
}
func (g Grid) isFullyVisible(i, j, di, dj int) bool {
for n := 1; !g.isOutOfBound(i+n*di, j+n*dj); n++ {
if g[i][j] <= g[i+n*di][j+n*dj] {
return false
}
}
return true
}
type DayImpl struct {
grid Grid
}
func (d *DayImpl) Init(lines []string) error {
for i, line := range lines {
d.grid = append(d.grid, nil)
for _, char := range line {
d.grid[i] = append(d.grid[i], int(char-'0'))
}
}
return nil
}
func (d *DayImpl) Part1() (string, error) {
visibles := 0
for i := 0; i < len(d.grid); i++ {
for j := 0; j < len(d.grid); j++ {
isVisible := false
isVisible = isVisible || d.grid.isFullyVisible(i, j, 0, 1)
isVisible = isVisible || d.grid.isFullyVisible(i, j, 0, -1)
isVisible = isVisible || d.grid.isFullyVisible(i, j, 1, 0)
isVisible = isVisible || d.grid.isFullyVisible(i, j, -1, 0)
if isVisible {
visibles++
}
}
}
return fmt.Sprint(visibles), nil
}
func (d *DayImpl) Part2() (string, error) {
maxScore := 0
for i := 0; i < len(d.grid); i++ {
for j := 0; j < len(d.grid); j++ {
score := 1
score *= d.grid.getVisibilityDistance(i, j, 0, 1)
score *= d.grid.getVisibilityDistance(i, j, 0, -1)
score *= d.grid.getVisibilityDistance(i, j, 1, 0)
score *= d.grid.getVisibilityDistance(i, j, -1, 0)
if score > maxScore {
maxScore = score
}
}
}
return fmt.Sprint(maxScore), nil
}
func main() {
utils.Run(&DayImpl{}, false)
}