-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart2.go
71 lines (68 loc) · 1.28 KB
/
part2.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
package main
import (
"aocli/utils/maps"
"aocli/utils/reader"
"image"
"slices"
)
func doPartTwo(input string) int {
mapper := maps.MakeImagePointMap(reader.FileLineByLine(input))
seen := make(map[image.Point]bool)
var delta = []image.Point{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
var res int
for i, r := range mapper {
if seen[i] {
continue
}
NP := []image.Point{i}
sides := make(map[image.Point][]image.Point)
var area int
for len(NP) > 0 {
P := NP[0]
NP = NP[1:]
if seen[P] {
continue
}
area++
seen[P] = true
for _, d := range delta {
np := P.Add(d)
if _, ok := mapper[np]; mapper[np] == r && ok {
NP = append(NP, np)
continue
}
if _, ok := sides[d]; !ok {
sides[d] = []image.Point{}
}
sides[d] = append(sides[d], P)
}
}
var S int
for _, s := range sides {
sideseen := make(map[image.Point]bool)
for _, i := range s {
if _, ok := sideseen[i]; ok {
continue
}
S++
NP := []image.Point{i}
for len(NP) > 0 {
P := NP[0]
NP = NP[1:]
if sideseen[P] {
continue
}
sideseen[P] = true
for _, dd := range delta {
np := P.Add(dd)
if slices.Contains(s, np) {
NP = append(NP, np)
}
}
}
}
}
res += area * S
}
return res
}