-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
231 lines (195 loc) · 5.86 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"fmt"
aoc "github.com/shraddhaag/aoc/library"
)
func main() {
input := aoc.Get2DGrid(aoc.ReadFileLineByLine("input.txt"))
ans1, ans2 := alternativeSolution(input)
fmt.Println("answer for part 1: ", ans1)
fmt.Println("answer for part 2: ", ans2)
}
type p struct {
x, y int
}
func checkAll4(input [][]string, current p) []p {
sameAround := []p{}
// can check left
if current.x > 0 && input[current.y][current.x-1] == input[current.y][current.x] {
sameAround = append(sameAround, p{current.x - 1, current.y})
}
// can check right
if current.x < len(input[0])-1 && input[current.y][current.x+1] == input[current.y][current.x] {
sameAround = append(sameAround, p{current.x + 1, current.y})
}
// can check up
if current.y > 0 && input[current.y-1][current.x] == input[current.y][current.x] {
sameAround = append(sameAround, p{current.x, current.y - 1})
}
// can check down
if current.y < len(input)-1 && input[current.y+1][current.x] == input[current.y][current.x] {
sameAround = append(sameAround, p{current.x, current.y + 1})
}
return sameAround
}
func ans(input [][]string) (int, int) {
cost, cost2 := 0, 0
visited := make(map[p]struct{})
for j, row := range input {
for i, _ := range row {
if _, ok := visited[p{i, j}]; ok {
continue
}
shape := findAllGardensRecursive(input, p{i, j}, polynomial{}, visited)
cost += shape.area * shape.perimeter
cost2 += shape.area * shape.sides
}
}
return cost, cost2
}
type polynomial struct {
area int
perimeter int
sides int
}
func findAllGardensRecursive(input [][]string, current p, shape polynomial, visited map[p]struct{}) polynomial {
if _, ok := visited[current]; ok {
return shape
}
checkNext := checkAll4(input, current)
// none surrounding are same garden
if len(checkNext) == 0 {
if shape.area == 0 {
shape.area = 1
shape.perimeter = 4
visited[current] = struct{}{}
shape.sides = checkCorners(input, current)
return shape
}
return shape
}
shape.perimeter += 4 - len(checkNext)
shape.area += 1
visited[current] = struct{}{}
shape.sides += checkCorners(input, current)
for _, next := range checkNext {
shape = findAllGardensRecursive(input, next, shape, visited)
}
return shape
}
// checkCorners checks if there are any corners on the
// current coordinates. It checks for both, outside
// and inside corners
func checkCorners(input [][]string, current p) int {
count := 0
gardenType := input[current.y][current.x]
x, y := current.x, current.y
if x == 0 && y == 0 {
count += 1
}
if x == 0 && y == len(input)-1 {
count += 1
}
if x == len(input[0])-1 && y == len(input)-1 {
count += 1
}
if x == len(input[0])-1 && y == 0 {
count += 1
}
// top left outside corner
// ## __ |#
// #O #O |O
if (x > 0 && y > 0 && input[y][x-1] != gardenType && input[y-1][x] != gardenType) ||
(x > 0 && y == 0 && input[y][x-1] != gardenType) || (x == 0 && y > 0 && input[y-1][x] != gardenType) {
count += 1
}
// top left inside corner
// OO
// O#
if x < len(input[0])-1 && y < len(input)-1 && input[y][x+1] == gardenType && input[y+1][x] == gardenType && input[y+1][x+1] != gardenType {
count += 1
}
// top right outside corner
// ## __ #|
// O# O# O|
if (x < len(input[0])-1 && y > 0 && input[y][x+1] != gardenType && input[y-1][x] != gardenType) ||
(x < len(input[0])-1 && y == 0 && input[y][x+1] != gardenType) || (x == len(input[0])-1 && y > 0 && input[y-1][x] != gardenType) {
count += 1
}
// top right inside corner
// OO
// #O
if x > 0 && y < len(input)-1 && input[y][x-1] == gardenType && input[y+1][x] == gardenType && input[y+1][x-1] != gardenType {
count += 1
}
// bottom left outside corner
// #O #O |O
// ## -- |#
if (x > 0 && y < len(input)-1 && input[y][x-1] != gardenType && input[y+1][x] != gardenType) ||
(x > 0 && y == len(input)-1 && input[y][x-1] != gardenType) || (x == 0 && y < len(input)-1 && input[y+1][x] != gardenType) {
count += 1
}
// bottom left inside corner
// O#
// OO
if x < len(input[0])-1 && y > 0 && input[y][x+1] == gardenType && input[y-1][x] == gardenType && input[y-1][x+1] != gardenType {
count += 1
}
// bottom right outside corner
// O# O# O|
// ## -- #|
if (x < len(input[0])-1 && y < len(input)-1 && input[y][x+1] != gardenType && input[y+1][x] != gardenType) ||
(x < len(input[0])-1 && y == len(input)-1 && input[y][x+1] != gardenType) || (x == len(input[0])-1 && y < len(input)-1 && input[y+1][x] != gardenType) {
count += 1
}
// bottom right inside corner
// #O
// OO
if x > 0 && y > 0 && input[y][x-1] == gardenType && input[y-1][x] == gardenType && input[y-1][x-1] != gardenType {
count += 1
}
return count
}
func alternativeSolution(input [][]string) (int, int) {
cost1, cost2 := 0, 0
visitedCoordinates := make(map[p]struct{})
for j, _ := range input {
for i, _ := range input[j] {
if _, ok := visitedCoordinates[p{i, j}]; !ok {
next := []p{{i, j}}
shape := polynomial{}
for len(next) != 0 {
newShape, traverseNext := findAllGardensNonRecursively(input, next[0], shape, visitedCoordinates)
shape = newShape
next = append(next, traverseNext...)
next = next[1:]
}
cost1 += shape.area * shape.perimeter
cost2 += shape.area * shape.sides
}
}
}
return cost1, cost2
}
func findAllGardensNonRecursively(input [][]string, current p, shape polynomial, visited map[p]struct{}) (polynomial, []p) {
if _, ok := visited[current]; ok {
return shape, []p{}
}
checkNext := checkAll4(input, current)
// none surrounding are same garden
if len(checkNext) == 0 {
if shape.area == 0 {
visited[current] = struct{}{}
shape = polynomial{
area: 1, perimeter: 4, sides: 4,
}
return shape, []p{}
}
return shape, []p{}
}
shape.perimeter += 4 - len(checkNext)
shape.area += 1
visited[current] = struct{}{}
shape.sides += checkCorners(input, current)
return shape, checkNext
}