-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (73 loc) · 1.53 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
84
85
86
87
88
89
90
package main
import (
"fmt"
"io"
"os"
"sort"
"github.com/devries/advent_of_code_2021/utils"
)
func main() {
f, err := os.Open("../inputs/day09.txt")
utils.Check(err, "error opening input.txt")
defer f.Close()
r := solve(f)
fmt.Println(r)
}
func solve(r io.Reader) int {
lines := utils.ReadLines(r)
grid := parseGrid(lines)
lowPoints := make([]utils.Point, 0)
outer:
for k, v := range grid {
for _, d := range utils.Directions {
if va, ok := grid[k.Add(d)]; ok {
if va <= v {
continue outer
}
}
}
lowPoints = append(lowPoints, k)
}
sizes := make([]int, 0)
for _, p := range lowPoints {
size := findBasin(p, grid)
sizes = append(sizes, size)
}
sort.Sort(sort.Reverse(sort.IntSlice(sizes)))
res := 1
for i := 0; i < 3; i++ {
res *= sizes[i]
}
return res
}
func parseGrid(lines []string) map[utils.Point]int {
r := make(map[utils.Point]int)
for j, ln := range lines {
for i, c := range ln {
r[utils.Point{X: i, Y: j}] = int(c - '0')
}
}
return r
}
func findBasin(p utils.Point, grid map[utils.Point]int) int {
basin := make([]utils.Point, 0)
height := grid[p]
basin = append(basin, p)
found := make(map[utils.Point]bool)
for i := height + 1; i < 9; i++ {
newBasin := make([]utils.Point, 0)
for _, v := range basin {
for _, d := range utils.Directions {
pc := v.Add(d)
if va, ok := grid[pc]; ok {
if va == i && !found[pc] {
newBasin = append(newBasin, pc)
found[pc] = true
}
}
}
}
basin = append(basin, newBasin...)
}
return len(basin)
}