-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pathfinding.go
210 lines (171 loc) · 4.06 KB
/
Pathfinding.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
package main
import (
"errors"
"fmt"
)
/*
Since a lot of my previous files were getting fairly large, I
decided to split off my pathfinding algorithms into a separate
file. Let's go!
*/
// Breadth-First-Search
//
// define State type
//
type State struct {
loc Coord // location of the elf
v Valley // valley object, the whole map at this slice
}
func NewState(c Coord, val Valley) State {
return State{
loc: c,
v: val,
}
}
//
// define queue
//
type Queue struct {
q []State
}
func (q *Queue) Length() int {
return len(q.q)
}
func (q *Queue) Push(s State) {
// adds explorer to queue
q.q = append(q.q, s)
}
func (q *Queue) Pop() (State, error) {
if len(q.q) == 0 {
return State{}, errors.New("empty queue")
}
ne := q.q[0]
q.q = q.q[1:]
return ne, nil
}
//
// Stack
//
type Stack struct {
s []State
}
func (s *Stack) Length() int {
return len(s.s)
}
func (s *Stack) Push(st State) {
// adds state to stack
s.s = append(s.s, st)
}
func (s *Stack) Pop() (State, error) {
if len(s.s) == 0 {
return State{}, errors.New("empty stack")
}
ne := s.s[len(s.s)-1]
s.s = s.s[:len(s.s)-1]
return ne, nil
}
//
// define breadcrumb
//
type exists struct{}
type Breadcrumb struct {
crumb map[string]exists
}
func (b *Breadcrumb) Add(hash string, c Coord) bool {
// this will place a breadcrumb. Will return false ONLY if
// the breadcrumb already exists!
// create the hash
newHash := fmt.Sprintf("%s|%d~%d", hash, c.row, c.col)
if _, ok := b.crumb[newHash]; ok {
return false
}
b.crumb[newHash] = exists{}
return true
}
//
// BFS
//
func Traverse(start Coord, val Valley) int {
// second time's a charm!
b := Breadcrumb{
crumb: make(map[string]exists),
}
// hello little explorer!
agr0 := NewState(start, val)
q := Queue{}
q.Push(agr0)
for q.Length() > 0 {
this, err := q.Pop()
if err != nil {
fmt.Printf("Empty queue!\n")
break
}
// let's do a quick check for exit status
if this.loc == this.v.Exit {
// we're at the exit, return total.
// fmt.Printf("We made it!\n")
return this.v.Minutes
}
// add and check the breadcrumb
if !b.Add(this.v.GetHash(), this.loc) {
// we are in the same state as previously defined, this would
// yield no additional positive outcomes, so bomb this one out.
// fmt.Printf("Hit a breadcrumb, blow this away\n")
continue
}
// otherwise, let's eval our options
options := this.v.GetValidDirections(this.loc)
// fmt.Printf("Found options from this loc: c:%d r:%d : %#v\n", this.loc.col, this.loc.row, options)
for _, value := range options {
// split off the realities!
altUniverse := NewState(value, this.v.DeepCopy())
altUniverse.v.MoveOne()
q.Push(altUniverse)
}
}
return 0
}
func TraverseAndReturn(currentState State, start, end Coord) (int, State) {
// this function operates similarly to the Traverse function, but it
// is now made to start at arbitrary positions.
b := Breadcrumb{
crumb: make(map[string]exists),
}
// // hello little explorer!
// agr0 := NewState(start, val)
q := Queue{}
q.Push(currentState)
// iter := 0
for q.Length() > 0 {
// iter++
this, err := q.Pop()
if err != nil {
fmt.Printf("Empty queue!\n")
break
}
// let's do a quick check for exit status
if this.loc == end {
// we're at the exit, return total.
// fmt.Printf("We made it!\n")
return this.v.Minutes, this
}
// add and check the breadcrumb
if !b.Add(this.v.GetHash(), this.loc) {
// we are in the same state as previously defined, this would
// yield no additional positive outcomes, so bomb this one out.
// fmt.Printf("Hit a breadcrumb, blow this away\n")
continue
}
// otherwise, let's eval our options
options := this.v.GetValidDirectionsPartTwo(this.loc, start, end)
// fmt.Printf("Found options from this loc: c:%d r:%d : %#v\n", this.loc.col, this.loc.row, options)
for _, value := range options {
// split off the realities!
altUniverse := NewState(value, this.v.DeepCopy())
altUniverse.v.MoveOne()
q.Push(altUniverse)
}
}
// fmt.Printf("Iterated: %d\n", iter)
return 0, State{} // we got nothin
}