-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.go
332 lines (288 loc) · 7.53 KB
/
day15.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"aoc2021/util"
"fmt"
"time"
)
func main() {
lines := util.GetLines("day15\\15.example")
start := time.Now()
partA(lines)
duration := time.Since(start)
partB(lines)
duration2 := time.Since(start)
fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
}
type point struct {
x, y int
}
var fScores = map[point]int{}
var gScores = map[point]int{}
const infinity = 2147483646
func getFScore(p point) int {
value, ok := fScores[p]
if !ok {
fScores[p] = infinity
return infinity
}
return value
}
func setFScore(p point, value int) {
fScores[p] = value
}
func getGScore(p point) int {
value, ok := gScores[p]
if !ok {
gScores[p] = infinity
return infinity
}
return value
}
func setGScore(p point, value int) {
gScores[p] = value
}
func h(p point, width, height int) int {
// dijkstra
return 0
return width - p.x + height - p.y - 2
}
func lowestFScore(points []point) (point, int) {
lowest := infinity
lowestP := points[0]
pos := -1
for i, p := range points {
fscore := getFScore(p)
if fscore < lowest {
lowest = fscore
lowestP = p
pos = i
}
}
return lowestP, pos
}
func removeFromPointSlice(slice []point, pos int) []point {
if len(slice) == 1 {
return []point{}
}
if pos < 0 || pos >= len(slice) {
fmt.Errorf("removed at invalid position from point slice")
}
newLength := len(slice) - 1
slice[pos] = slice[newLength] // replace with last element
return slice[:newLength]
}
func giveNeighbors(width, height int, p point) []point {
neighbors := []point{}
// no diag-cells included
offsets := []point{
{-1, 0}, {1, 0},
{0, -1}, {0, 1},
}
for _, offset := range offsets {
x := p.x + offset.x
y := p.y + offset.y
if x >= 0 && y >= 0 && x < width && y < height {
neighbors = append(neighbors, point{x, y})
}
}
return neighbors
}
func addToPointSet(newP point, slice []point) []point {
// Naive implementaion
for _, p := range slice {
if p == newP {
return slice
}
}
return append(slice, newP)
}
func addToPrioPointQueue(newP point, queue []point) []point {
//sorted by lowest fscore
if len(queue) == 0 {
fmt.Printf("Was empty, adding %v\n", newP)
return []point{newP}
}
currentFScore := getFScore(newP)
newQueue := []point{}
notInserted := true
for _, el := range queue {
if el == newP {
continue
}
if notInserted && getFScore(el) >= currentFScore {
newQueue = append(newQueue, newP)
notInserted = false
}
newQueue = append(newQueue, el)
}
if notInserted {
newQueue = append(newQueue, newP)
}
fmt.Println(queue)
for _, el := range newQueue {
fmt.Printf("Point: %v, FScore: %d\n", el, getFScore(el))
}
return newQueue
}
func givePriorityElementAndNewQueue(queue []point) (point, []point) {
fmt.Printf("giving back: %v and %v\n", queue[0], len(queue[1:]))
return queue[0], queue[1:]
}
func reconstructPath(cameFrom map[point]point, n, start point) []point {
path := []point{n}
current := n
for current != start {
previous := cameFrom[current]
path = append(path, previous)
current = previous
}
return path
}
func pointIncludedInSlice(slice []point, candidate point) bool {
for _, p := range slice {
if p == candidate {
return true
}
}
return false
}
func partA(lines []string) {
// setting up the map with its values
width := len(lines[0])
height := len(lines)
pointScores := make([][]int, height)
for i := 0; i < width; i++ {
pointScores[i] = make([]int, width)
}
for i, line := range lines {
for j, value := range line {
pointScores[i][j] = int(value - '0')
}
}
// setting up A*
start := point{0, 0}
goal := point{height - 1, width - 1}
openSet := []point{start}
cameFrom := map[point]point{}
path := []point{}
setGScore(start, 0)
setFScore(start, h(start, width, height))
for len(openSet) > 0 {
// check next in line with lowest score
current, pos := lowestFScore(openSet)
if current == goal {
path = reconstructPath(cameFrom, current, start)
break
}
// remove from openSet
openSet = removeFromPointSlice(openSet, pos)
// check neighbors
neighbors := giveNeighbors(width, height, current)
currentGScore := getGScore(current)
for _, n := range neighbors {
tentativeGScore := currentGScore + pointScores[n.y][n.x]
if tentativeGScore < getGScore(n) {
setGScore(n, tentativeGScore)
setFScore(n, tentativeGScore+h(n, width, height))
cameFrom[n] = current
openSet = addToPointSet(n, openSet)
}
}
}
//printPath(path, height, width, pointScores)
totalScore := calculateScore(path, pointScores)
fmt.Printf("Solution for part A: %v\n", totalScore)
}
func printPath(path []point, height, width int, pointScores [][]int) {
for i := 0; i < height; i++ {
for j := 0; j < width; j++ {
onPath := pointIncludedInSlice(path, point{j, i})
if onPath {
fmt.Print("\033[33m", getPointScore(pointScores, j, i), "\033[0m")
} else {
fmt.Print(getPointScore(pointScores, j, i))
}
}
fmt.Println()
}
}
func calculateScore(path []point, pointScores [][]int) int {
score := -getPointScore(pointScores, 0, 0)
for _, p := range path {
score += getPointScore(pointScores, p.x, p.y)
}
return score
}
func getPointScore(pointScores [][]int, x, y int) int {
height := len(pointScores)
width := len(pointScores[0])
if x < width && y < height {
return pointScores[y][x]
}
addX := x / width
addY := y / width
newValue := pointScores[y%height][x%width] + addX + addY
for newValue > 9 {
newValue -= 9
}
return newValue
}
func partB(lines []string) {
// reset globals
fScores = map[point]int{}
gScores = map[point]int{}
// setting up the map with its values
width := len(lines[0])
height := len(lines)
pointScores := make([][]int, height)
for i := 0; i < width; i++ {
pointScores[i] = make([]int, width)
}
for i, line := range lines {
for j, value := range line {
pointScores[i][j] = int(value - '0')
}
}
// scale map
width, height = width*5, height*5
// setting up A*
start := point{0, 0}
goal := point{height - 1, width - 1}
openSet := []point{start}
cameFrom := map[point]point{}
path := []point{}
setGScore(start, 0)
setFScore(start, h(start, width, height))
for len(openSet) > 0 {
fmt.Println("New Iteration")
fmt.Println(openSet)
// check next in line with lowest score
//current, pos := lowestFScore(openSet)
current, openSet := givePriorityElementAndNewQueue(openSet)
if current == goal {
path = reconstructPath(cameFrom, current, start)
break
}
fmt.Println(openSet)
// remove from openSet
//openSet = removeFromPointSlice(openSet, pos)
// check neighbors
neighbors := giveNeighbors(width, height, current)
currentGScore := getGScore(current)
for _, n := range neighbors {
fmt.Printf("Checking element: %v\n", n)
tentativeGScore := currentGScore + getPointScore(pointScores, n.x, n.y)
fmt.Printf("Tentative: %v, GScore %v\n", tentativeGScore, getGScore(n))
if tentativeGScore < getGScore(n) {
setGScore(n, tentativeGScore)
setFScore(n, tentativeGScore+h(n, width, height))
cameFrom[n] = current
openSet = addToPrioPointQueue(n, openSet)
fmt.Printf("Added element: %v, newQueue: %v\n", n, openSet)
}
}
}
//printPath(path, height, width, pointScores)
totalScore := calculateScore(path, pointScores)
fmt.Printf("Solution for part B: %v\n", totalScore)
}