var ret = make([]int, len(lows))
var seen = Seen{}
for i, item := range lows {
ret[i] = seen.getBasinSize(m, item)
}
sort.Ints(ret)
return ret[len(ret)-1] * ret[len(ret)-2] * ret[len(ret)-3]
}
func (seen Seen) getBasinSize(m Matrix, item [2]int) int {
var ret int
row, col := item[0], item[1]
if seen[item] || m[row][col] == 9 {
return 0
}
seen[item] = true
ret++
for _, d := range directions {
if row+d[0] >= 0 && row+d[0] < len(m) &&
col+d[1] >= 0 && col+d[1] < len(m[0]) {
if m[row+d[0]][col+d[1]] != 9 {
ret += seen.getBasinSize(m, [2]int{row + d[0], col + d[1]})
}
}
}
return ret
}
func (m Matrix) getLows() [][2]int {
var ret [][2]int
for row, line := range m {
for col := range line {
if m.checkLow(row, col) {
ret = append(ret, [2]int{row, col})
}
}
}
return ret
}