-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathb.go
78 lines (62 loc) · 1.36 KB
/
b.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
package day09
import (
"sort"
"strings"
)
type PuzzleB struct{}
func (p PuzzleB) String() string {
return "09b"
}
func (p PuzzleB) Run() int {
grid := make([][]int, GridSizeY)
for i := range grid {
grid[i] = make([]int, GridSizeX)
}
lines := strings.Split(input, "\n")
for i := range lines {
for j, c := range lines[i] {
grid[i][j] = int(c - '0')
}
}
// Just wanted to see if this possible in Go, using recursion on an anonymous function
var searchBasin func(x, y int) int
searchBasin = func(x, y int) int {
c := 0
// Recursively search all neighbours, marking each visited point with a 9
// to ensure it is not traversed again
grid[y][x] = 9
// Top
if y >= 1 && grid[y-1][x] < 9 {
c += searchBasin(x, y-1) + 1
}
// Left
if x >= 1 && grid[y][x-1] < 9 {
c += searchBasin(x-1, y) + 1
}
// Right
if x < GridSizeX-1 && grid[y][x+1] < 9 {
c += searchBasin(x+1, y) + 1
}
// Down
if y < GridSizeY-1 && grid[y+1][x] < 9 {
c += searchBasin(x, y+1) + 1
}
return c
}
basins := []int{}
for y := range grid {
for x := range grid[y] {
// A basin is identified by the first non 9 number
if grid[y][x] < 9 {
b := searchBasin(x, y) + 1
basins = append(basins, b)
}
}
}
sort.Ints(basins)
t := basins[len(basins)-1]
for _, v := range basins[len(basins)-3 : len(basins)-1] {
t *= v
}
return t
}