-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
194 lines (153 loc) · 3.22 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"sort"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2021", "9", "input.txt"))
if err != nil {
panic(err)
}
return input
}
func solve(r io.Reader) {
scanner := bufio.NewScanner(r)
var cave [][]int
for scanner.Scan() {
line := scanner.Text()
var row []int
for _, rawHeight := range line {
height := rawHeight - 48
row = append(row, int(height))
}
cave = append(cave, row)
}
if scanner.Err() != nil {
panic(scanner.Err())
}
result := assignBasins(cave)
basins := getBasins(cave)
sizes := basinSizes(result, basins)
fmt.Println(sizes, sizes[0]*sizes[1]*sizes[2])
}
type pos struct {
i, j int
}
func basinSizes(assignment [][]pos, basins []pos) []int {
var ret []int
for _, basin := range basins {
ret = append(ret, count(assignment, basin))
}
sort.Slice(ret, func(i, j int) bool {
return ret[i] > ret[j]
})
return ret
}
func count(assignment [][]pos, basin pos) int {
sum := 0
for i := 0; i < len(assignment); i++ {
for j := 0; j < len(assignment[i]); j++ {
if assignment[i][j] == basin {
sum += 1
}
}
}
return sum
}
func assignBasins(cave [][]int) [][]pos {
assignment := make([][]pos, len(cave))
for i, row := range cave {
assignment[i] = make([]pos, len(row))
for j := range row {
assignment[i][j] = pos{
i: -1,
j: -1,
}
}
}
for i := 0; i < len(cave); i++ {
for j := 0; j < len(cave[i]); j++ {
flow(cave, assignment, i, j)
}
}
return assignment
}
var sentinel = pos{i: -1, j: -1}
// to optimize, instead of starting from each point and moving lower, start from each low point and move up
func flow(cave [][]int, assignment [][]pos, i, j int) pos {
if assignment[i][j] != sentinel || cave[i][j] == 9 {
return assignment[i][j]
}
values, positions := adjacent(cave, i, j)
smaller, ok := firstSmallerPos(cave[i][j], values, positions)
if !ok {
assignment[i][j] = pos{i, j}
return pos{i, j}
}
basin := flow(cave, assignment, smaller.i, smaller.j)
assignment[i][j] = basin
return basin
}
func firstSmallerPos(v int, values []int, positions []pos) (pos, bool) {
for i, vv := range values {
if vv < v {
return positions[i], true
}
}
return pos{}, false
}
func getBasins(cave [][]int) []pos {
var ret []pos
for i := 0; i < len(cave); i++ {
for j := 0; j < len(cave[i]); j++ {
vals, _ := adjacent(cave, i, j)
if allSmaller(cave[i][j], vals) {
ret = append(ret, pos{i: i, j: j})
}
}
}
return ret
}
func allSmaller(v int, values []int) bool {
for _, vv := range values {
if v >= vv {
return false
}
}
return true
}
func adjacent(cave [][]int, i, j int) ([]int, []pos) {
var ret []int
var ret2 []pos
for _, offset := range []pos{
{1, 0},
{0, 1},
{-1, 0},
{0, -1},
} {
v, ok := get(cave, i+offset.i, j+offset.j)
if ok {
ret = append(ret, v)
ret2 = append(ret2, pos{
i: i + offset.i,
j: j + offset.j,
})
}
}
return ret, ret2
}
func get(cave [][]int, i, j int) (int, bool) {
if i < 0 || j < 0 || i > len(cave)-1 || j > len(cave[0])-1 {
return 0, false
}
return cave[i][j], true
}
func main() {
solve(strings.NewReader("2199943210\n3987894921\n9856789892\n8767896789\n9899965678"))
solve(input())
}