-
Notifications
You must be signed in to change notification settings - Fork 0
/
d12.go
177 lines (157 loc) · 3.55 KB
/
d12.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
package main
import (
"sort"
"strconv"
"strings"
)
type d12garden struct {
garden []string
width int64
height int64
memo map[int64]*d12region
regions []*d12region
}
type d12region struct {
garden *d12garden
plant byte
plots []coords
}
func (g *d12garden) idx(x, y int64) int64 {
return y*g.width + x
}
func (g *d12garden) isScanned(x, y int64) bool {
_, done := g.memo[g.idx(x, y)]
return done
}
func (g *d12garden) getPlot(x, y int64) byte {
return g.garden[y][x]
}
func (g *d12garden) isWithinBounds(x, y int64) bool {
return x >= 0 && y >= 0 && x < g.width && y < g.height
}
func (g *d12garden) scan(x, y int64, plant byte, region *d12region) {
if !g.isWithinBounds(x, y) || g.isScanned(x, y) {
return
}
if g.getPlot(x, y) != plant {
return
}
g.memo[g.idx(x, y)] = region
region.plots = append(region.plots, coords{x: x, y: y})
g.scan(x+1, y, plant, region)
g.scan(x-1, y, plant, region)
g.scan(x, y-1, plant, region)
g.scan(x, y+1, plant, region)
}
func (r *d12region) isWithinRegion(x, y int64) bool {
return r.garden.memo[r.garden.idx(x, y)] == r
}
func (r *d12region) getPerimeter() (perimeter int) {
for _, p := range r.plots {
sides := 4
if r.isWithinRegion(p.x+1, p.y) {
sides--
}
if r.isWithinRegion(p.x-1, p.y) {
sides--
}
if r.isWithinRegion(p.x, p.y+1) {
sides--
}
if r.isWithinRegion(p.x, p.y-1) {
sides--
}
perimeter += sides
}
return
}
func (r *d12region) getSides() (sides int) {
left := make(map[int64][]int64)
right := make(map[int64][]int64)
top := make(map[int64][]int64)
uwu := make(map[int64][]int64)
for _, p := range r.plots {
if !r.isWithinRegion(p.x+1, p.y) {
right[p.x] = append(right[p.x], p.y)
}
if !r.isWithinRegion(p.x-1, p.y) {
left[p.x] = append(left[p.x], p.y)
}
if !r.isWithinRegion(p.x, p.y+1) {
top[p.y] = append(top[p.y], p.x)
}
if !r.isWithinRegion(p.x, p.y-1) {
uwu[p.y] = append(uwu[p.y], p.x)
}
}
maps := []map[int64][]int64{left, right, top, uwu}
for _, m := range maps {
for _, s := range m {
sort.Slice(s, func(i, j int) bool {
return s[i] < s[j]
})
last := int64(-2)
for _, c := range s {
if c != last+1 && c != last {
sides++
}
last = c
}
}
}
return
}
func (r *d12region) calculatePrice(discount bool) int {
if discount {
return len(r.plots) * r.getSides()
}
return len(r.plots) * r.getPerimeter()
}
func (*methods) D12P1(input string) string {
lines := strings.Split(input, "\n")
g := &d12garden{
garden: lines,
width: int64(len(lines[0])),
height: int64(len(lines)),
memo: make(map[int64]*d12region),
}
for y, row := range g.garden {
for x, plot := range row {
if g.isScanned(int64(x), int64(y)) {
continue
}
region := &d12region{garden: g, plant: byte(plot)}
g.scan(int64(x), int64(y), byte(plot), region)
g.regions = append(g.regions, region)
}
}
var price int
for _, r := range g.regions {
price += r.calculatePrice(false)
}
return strconv.Itoa(price)
}
func (*methods) D12P2(input string) string {
lines := strings.Split(input, "\n")
g := &d12garden{
garden: lines,
width: int64(len(lines[0])),
height: int64(len(lines)),
memo: make(map[int64]*d12region),
}
for y, row := range g.garden {
for x, plot := range row {
if g.isScanned(int64(x), int64(y)) {
continue
}
region := &d12region{garden: g, plant: byte(plot)}
g.scan(int64(x), int64(y), byte(plot), region)
g.regions = append(g.regions, region)
}
}
var price int
for _, r := range g.regions {
price += r.calculatePrice(true)
}
return strconv.Itoa(price)
}