aboutsummaryrefslogtreecommitdiff
path: root/2021/09/second.go
blob: 6cba3dd80c2799d6c749a115ac5764f59d89407b (plain)
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
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var (
	grid          [][]byte = make([][]byte, 0)
	width, height int
)

func isMin(x, y int) bool {
	var left, right, top, bottom byte
	if x == 0 {
		left = '9'
	} else {
		left = grid[x-1][y]
	}
	if x == height-1 {
		right = '9'
	} else {
		right = grid[x+1][y]
	}
	if y == 0 {
		top = '9'
	} else {
		top = grid[x][y-1]
	}
	if y == width-1 {
		bottom = '9'
	} else {
		bottom = grid[x][y+1]
	}
	return grid[x][y] < top && grid[x][y] < bottom && grid[x][y] < left && grid[x][y] < right
}

type Min struct {
	x, y int
}

var minimums []Min

func main() {
	s := bufio.NewScanner(os.Stdin)
	s.Scan()
	width = len(s.Text())
	for {
		line := make([]byte, width)
		for i, ch := range s.Text() {
			line[i] = byte(ch)
		}
		grid = append(grid, line)
		if !s.Scan() {
			break
		}
	}
	height = len(grid)

	for y := 0; y < width; y++ {
		for x := 0; x < height; x++ {
			if isMin(x, y) {
				minimums = append(minimums, Min{x, y})
			}
		}
	}

	basins := make([]int, 0)
	for _, m := range minimums {
		basins = append(basins, computeBasinSize(m.x, m.y))
	}
	sort.Ints(basins)
	basins = basins[len(basins)-3:]
	fmt.Println(basins[0] * basins[1] * basins[2])
}

func computeBasinSize(x, y int) int {
	if x < 0 || x >= height || y < 0 || y >= width || grid[x][y] == '9' {
		return 0
	}
	grid[x][y] = '9'
	return 1 + computeBasinSize(x-1, y) + computeBasinSize(x+1, y) + computeBasinSize(x, y-1) + computeBasinSize(x, y+1)
}