-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
194 lines (157 loc) · 4.12 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
package main
import (
"fmt"
"log"
"github.com/benallen-dev/advent-of-code-2023/pkg/color"
)
var largeInt int = 10000000000 // But still less than MaxInt to avoid overflow
func hasUnvisited(seen []bool, dists []int) bool {
for i := range seen {
if !seen[i] && dists[i] < 10000000000 {
return true
}
}
return false
}
func getLowestUnvisited(seen []bool, dists []int) int {
index := -1
lowest := largeInt
// O(N^2) so let's hope part 2 doesn't blow up the input size
for i := range seen {
if !seen[i] && dists[i] < lowest {
log.Printf("Found new lowest: %d", dists[i])
index = i
lowest = dists[i]
}
}
return index
}
func contains(arr []int, val int) bool {
for _, v := range arr {
if v == val {
return true
}
}
return false
}
func printGrid(lines []string, path []int) {
out := ""
for i, line := range lines {
for j, char := range line {
index := (i * len(lines[0])) + j
// if in path, add a color
if contains(path, index) {
out += fmt.Sprintf("%s%c%s", color.Red, char, color.Reset)
} else {
out += fmt.Sprintf("%c", char)
}
}
out += "\n"
}
log.Printf("Grid:\n%s", out)
}
func isFourthMove(curr int, lineLength int, next int, prev []int) (isFourthStep bool) {
previous := prev[curr]
if previous == -1 {
return false
}
compareMe := prev[previous]
if compareMe == -1 {
return false
}
// Based on which direction we're going, check prev[prev[prev[curr]]] - if it's in the same direction discard this move
if next == curr+1 {
// coming from Left
coord := next % lineLength
compareCoord := compareMe % lineLength
log.Printf("coord: %d, compareCoord: %d", coord, compareCoord)
if coord == compareCoord+3 {
return true
}
} else if next == curr-1 {
// Coming from right
coord := next % lineLength
compareCoord := compareMe % lineLength
if coord == compareCoord-3 {
return true
}
} else if next == curr+lineLength {
// coming from top
coord := next / lineLength
compareCoord := compareMe / lineLength
if coord == compareCoord+3 {
return true
}
} else if next == curr-lineLength {
// coming from bottom
coord := next / lineLength
compareCoord := compareMe / lineLength
if coord == compareCoord-3 {
return true
}
}
return false
}
func main() {
log.SetPrefix(color.Green + "[ # 17 ] " + color.Reset)
log.SetFlags(0)
// The supplied input can be interpreted as a 2D grid of node weights, where
// we want to find a path with the lowest weight. Apart from the limitation
// on the number of consecutive steps in a single direction, this is a
// shortest path problem ,so time to break out Dijkstra's algorithm.
adjs, lines := readInput("example.txt")
lineLength := len(lines[0])
// Fill dists with near-as-makes-no-difference infinity
dists := make([]int, len(adjs))
seen := make([]bool, len(adjs))
prev := make([]int, len(adjs))
for i := range adjs {
dists[i] = largeInt
seen[i] = false
prev[i] = -1
}
source := 0
target := len(adjs) - 1
dists[source] = 0
log.Printf("Source: %d, Target: %d", source, target)
curr := source
for curr != -1 {
curr = getLowestUnvisited(seen, dists)
if curr == -1 {
continue
}
// log.Printf("Current node: %d", curr)
seen[curr] = true
for _, edge := range adjs[curr] {
if !seen[edge.to] {
dist := dists[curr] + edge.weight
// Make sure this is not the 4th step in a row in the same direction
isFourthStep := isFourthMove(curr, lineLength, edge.to, prev)
if isFourthStep {
log.Printf("Skipping node[%d] because it's the 4th step in a row", edge.to)
continue
}
if dist < dists[edge.to] {
log.Printf("setting node[%d] to %d (was %d)", edge.to, dist, dists[edge.to])
dists[edge.to] = dist
prev[edge.to] = curr
}
}
}
}
pathNode := target
path := []int{pathNode}
for prev[pathNode] != -1 {
pathNode = prev[pathNode]
path = append(path, pathNode)
}
// Reverse the path
for i := len(path)/2 - 1; i >= 0; i-- {
opp := len(path) - 1 - i
path[i], path[opp] = path[opp], path[i]
}
log.Printf("Path: %v", path)
printGrid(lines, path)
totalDistance := dists[target]
log.Printf("Total distance: %d", totalDistance)
}