-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
205 lines (183 loc) · 3.73 KB
/
main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// NOTE: Part 2 is absolutely disgusting!
package main
import (
"fmt"
"github.com/devkvlt/aoc"
)
var (
grid [][]byte
height int
width int
)
func init() {
lines := aoc.Lines("input")
height = len(lines)
width = len(lines[0])
grid = make([][]byte, len(lines))
for i, line := range lines {
grid[i] = []byte(line)
}
}
type Pos struct{ x, y int }
func valid(p Pos) bool { return 0 <= p.x && p.x < width && 0 <= p.y && p.y < height }
func at(p Pos) byte { return grid[p.y][p.x] }
func up(p Pos) Pos { return Pos{p.x, p.y - 1} }
func down(p Pos) Pos { return Pos{p.x, p.y + 1} }
func left(p Pos) Pos { return Pos{p.x - 1, p.y} }
func right(p Pos) Pos { return Pos{p.x + 1, p.y} }
func neighbors(p Pos) []Pos {
var neighbors []Pos
for _, p_ := range []Pos{up(p), down(p), left(p), right(p)} {
if valid(p_) && at(p_) == at(p) {
neighbors = append(neighbors, p_)
}
}
return neighbors
}
func perimeter(p Pos) int {
perim := 0
atp := at(p)
if p.x == 0 || at(Pos{p.x - 1, p.y}) != atp {
perim++
}
if p.x == width-1 || at(Pos{p.x + 1, p.y}) != atp {
perim++
}
if p.y == 0 || at(Pos{p.x, p.y - 1}) != atp {
perim++
}
if p.y == height-1 || at(Pos{p.x, p.y + 1}) != atp {
perim++
}
return perim
}
func part1() {
seen := map[Pos]bool{}
queued := map[Pos]bool{}
sum := 0
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
p := Pos{x, y}
if seen[p] {
continue
}
area := 0
perim := 0
queue := []Pos{p}
queued[p] = true
for len(queue) > 0 {
curr := queue[0]
queue = queue[1:]
seen[curr] = true
area++
perim += perimeter(curr)
for _, n := range neighbors(curr) {
if !seen[n] && !queued[n] {
queued[n] = true
queue = append(queue, n)
}
}
}
sum += area * perim
}
}
fmt.Println(sum)
}
func cornerMap(p Pos) map[Pos]int {
m := map[Pos]int{}
other := func(p_ Pos) bool { return !valid(p_) || at(p_) != at(p) }
type X struct {
dir1 func(Pos) Pos
dir2 func(Pos) Pos
cornerPos Pos
}
xs := []X{
{up, left, p},
{up, right, Pos{p.x + 1, p.y}},
{down, left, Pos{p.x, p.y + 1}},
{down, right, Pos{p.x + 1, p.y + 1}},
}
for _, x := range xs {
if other(x.dir1(p)) {
m[x.cornerPos]++
}
if other(x.dir2(p)) {
m[x.cornerPos]++
}
if other(x.dir1(x.dir2(p))) {
m[x.cornerPos]--
if m[x.cornerPos] == -1 {
m[x.cornerPos] = 1
}
}
}
return m
}
func part2() {
type Plot struct {
typ byte
area int
cornerMap map[Pos]int
}
garden := []Plot{}
seen := map[Pos]bool{}
queued := map[Pos]bool{}
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
p := Pos{x, y}
if seen[p] {
continue
}
area := 0
m := map[Pos]int{}
queue := []Pos{p}
queued[p] = true
for len(queue) > 0 {
curr := queue[0]
queue = queue[1:]
seen[curr] = true
area++
for pos, count := range cornerMap(curr) {
if _, ok := m[pos]; !ok {
m[pos] += count
}
}
for _, n := range neighbors(curr) {
if !seen[n] && !queued[n] {
queued[n] = true
queue = append(queue, n)
}
}
}
garden = append(garden, Plot{at(p), area, m})
}
}
for i := 0; i < len(garden); i++ {
for j := i + 1; j < len(garden); j++ {
if garden[i].typ == garden[j].typ { // different plot but same plot type
// decrement shared corners
for p1 := range garden[i].cornerMap {
for p2 := range garden[j].cornerMap {
if p1 == p2 {
garden[i].cornerMap[p1]--
garden[j].cornerMap[p1]--
}
}
}
}
}
}
sum := 0
for _, plot := range garden {
sides := 0
for p := range plot.cornerMap {
sides += plot.cornerMap[p]
}
sum += plot.area * sides
}
fmt.Println(sum)
}
func main() {
part1() // 1483212
part2() // 897062
}