-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (130 loc) · 4.01 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
package main
import (
"fmt"
pq "github.com/emirpasic/gods/queues/priorityqueue"
aoc "github.com/shraddhaag/aoc/library"
)
func main() {
input := aoc.Get2DGrid(aoc.ReadFileLineByLine("input.txt"))
score, path := dijktras(input)
fmt.Println("answer for part 1: ", score)
fmt.Println("answer for part 2: ", getUniqueCount(input, path))
}
type step struct {
co aoc.Coordinates
lastDir aoc.Coordinates
score int
path map[aoc.Coordinates]int
}
type point struct {
co aoc.Coordinates
lastDir aoc.Coordinates
}
func dijktras(matrix [][]string) (int, map[aoc.Coordinates]int) {
priorityQueue := pq.NewWith(func(a, b interface{}) int {
return a.(step).score - b.(step).score
})
priorityQueue.Enqueue(step{aoc.Coordinates{1, len(matrix) - 2}, aoc.Right, 0, make(map[aoc.Coordinates]int)})
visited := make(map[point]struct{})
for !priorityQueue.Empty() {
element, _ := priorityQueue.Dequeue()
currentNode := element.(step)
if _, ok := visited[point{currentNode.co, currentNode.lastDir}]; ok {
continue
}
currentNode.path[currentNode.co] = currentNode.score
if matrix[currentNode.co.Y][currentNode.co.X] == "E" {
return currentNode.score, currentNode.path
}
nextSteps := getNextSteps(currentNode, matrix, visited)
for _, n := range nextSteps {
priorityQueue.Enqueue(n)
}
visited[point{currentNode.co, currentNode.lastDir}] = struct{}{}
}
return -1, make(map[aoc.Coordinates]int)
}
func isValidStep(current aoc.Coordinates, input [][]string) bool {
if current.X < 0 || current.Y < 0 || current.X >= len(input[0]) || current.Y >= len(input) {
return false
}
return true
}
func copyMap(path map[aoc.Coordinates]int) map[aoc.Coordinates]int {
new := make(map[aoc.Coordinates]int, len(path))
for key, value := range path {
new[key] = value
}
return new
}
func getAllowedDirections(direction aoc.Coordinates) []aoc.Coordinates {
switch direction {
case aoc.Up:
return []aoc.Coordinates{aoc.Up, aoc.Left, aoc.Right}
case aoc.Down:
return []aoc.Coordinates{aoc.Down, aoc.Left, aoc.Right}
case aoc.Left:
return []aoc.Coordinates{aoc.Up, aoc.Left, aoc.Down}
case aoc.Right:
return []aoc.Coordinates{aoc.Up, aoc.Down, aoc.Right}
}
return []aoc.Coordinates{}
}
func getNextSteps(current step, grid [][]string, visited map[point]struct{}) []step {
possibleNext := []step{}
for _, dir := range getAllowedDirections(current.lastDir) {
newPosition := aoc.Coordinates{current.co.X + dir.X, current.co.Y + dir.Y}
if !isValidStep(newPosition, grid) {
continue
}
if grid[newPosition.Y][newPosition.X] == "#" {
continue
}
if _, ok := visited[point{newPosition, dir}]; ok {
continue
}
score := current.score + 1
if dir != current.lastDir {
score += 1000
}
possibleNext = append(possibleNext, step{
co: newPosition,
lastDir: dir,
score: score,
path: copyMap(current.path),
})
}
return possibleNext
}
func getUniqueCount(matrix [][]string, path map[aoc.Coordinates]int) int {
priorityQueue := pq.NewWith(func(a, b interface{}) int {
return a.(step).score - b.(step).score
})
priorityQueue.Enqueue(step{aoc.Coordinates{1, len(matrix) - 2}, aoc.Right, 0, make(map[aoc.Coordinates]int)})
visited := make(map[point]struct{})
newSafeCoordinates := make(map[aoc.Coordinates]struct{})
for !priorityQueue.Empty() {
element, _ := priorityQueue.Dequeue()
currentNode := element.(step)
if score, ok := path[currentNode.co]; ok && score == currentNode.score {
for point, _ := range currentNode.path {
if _, ok := path[point]; !ok {
newSafeCoordinates[point] = struct{}{}
}
}
}
if _, ok := visited[point{currentNode.co, currentNode.lastDir}]; ok {
continue
}
currentNode.path[currentNode.co] = currentNode.score
if matrix[currentNode.co.Y][currentNode.co.X] == "E" {
continue
}
nextSteps := getNextSteps(currentNode, matrix, visited)
for _, n := range nextSteps {
priorityQueue.Enqueue(n)
}
visited[point{currentNode.co, currentNode.lastDir}] = struct{}{}
}
return len(path) + len(newSafeCoordinates)
}