-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
214 lines (182 loc) · 5.09 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
package main
import (
"container/heap"
"fmt"
"github.com/Alex-Waring/AoC/utils"
)
type Path struct {
loc utils.Location
steps int
turns int
}
func part1(input []string) int {
defer utils.Timer("part1")()
board := utils.NewBoard()
start := utils.NewLocation(0, 0, utils.Right)
for row, line := range input {
for col, char := range line {
board[utils.NewPosition(row, col)] = string(char)
if char == 'S' {
start = utils.NewLocation(row, col, utils.Right)
}
}
}
pq := make(utils.PriorityQueue, 1)
pq[0] = utils.NewItem(start, 0, 0)
heap.Init(&pq)
cost_so_far := map[utils.Location]int{}
cost_so_far[start] = 0
for pq.Len() > 0 {
item := heap.Pop(&pq).(*utils.Item)
loc := item.GetValue().(utils.Location)
cost := item.GetPriority()
// Check if finished
if board[loc.Pos] == "E" {
fmt.Println(cost)
return cost
}
// Try to move in the directin we're facing
newLoc := utils.Location{Pos: loc.Pos.Move(loc.Dir, 1), Dir: loc.Dir}
new_cost := cost + 1
if board[newLoc.Pos] != "#" {
if cost, ok := cost_so_far[newLoc]; !ok || cost > new_cost {
cost_so_far[newLoc] = new_cost
new_item := utils.NewItem(newLoc, new_cost, 0)
heap.Push(&pq, new_item)
pq.Update(new_item, new_cost)
}
}
// Try to turn left
newLoc = utils.Location{Pos: loc.Pos, Dir: loc.Dir.Turn(utils.Left)}
new_cost = cost + 1000
if cost, ok := cost_so_far[newLoc]; !ok || cost > new_cost {
cost_so_far[newLoc] = new_cost
new_item := utils.NewItem(newLoc, new_cost, 0)
heap.Push(&pq, new_item)
pq.Update(new_item, new_cost)
}
// Try to turn right
newLoc = utils.Location{Pos: loc.Pos, Dir: loc.Dir.Turn(utils.Right)}
new_cost = cost + 1000
if cost, ok := cost_so_far[newLoc]; !ok || cost > new_cost {
cost_so_far[newLoc] = new_cost
new_item := utils.NewItem(newLoc, new_cost, 0)
heap.Push(&pq, new_item)
pq.Update(new_item, new_cost)
}
}
panic("No solution found")
}
type StoredPath struct {
loc utils.Location
path map[utils.Position]bool
cost int
}
func part2(input []string, min int) {
defer utils.Timer("part2")()
board := utils.NewBoard()
start := utils.NewLocation(0, 0, utils.Right)
end := utils.NewPosition(0, 0)
for row, line := range input {
for col, char := range line {
board[utils.NewPosition(row, col)] = string(char)
if char == 'S' {
start = utils.NewLocation(row, col, utils.Right)
}
if char == 'E' {
end = utils.NewPosition(row, col)
}
}
}
pq := make(utils.PriorityQueue, 1)
pq[0] = utils.NewItem(StoredPath{
loc: start,
path: map[utils.Position]bool{start.Pos: true},
cost: 0,
}, end.Manhattan(start.Pos), 0)
heap.Init(&pq)
cost_so_far := map[utils.Location]int{}
cost_so_far[start] = 0
finishing_paths := []StoredPath{}
for pq.Len() > 0 {
item := heap.Pop(&pq).(*utils.Item)
item_value := item.GetValue().(StoredPath)
loc := item_value.loc
cost := item_value.cost
// Check if finished
if board[item_value.loc.Pos] == "E" {
finishing_paths = append(finishing_paths, item_value)
continue
}
// If we are over the minimum, we can stop
if cost > min {
continue
}
// Try to move in the directin we're facing
newLoc := utils.Location{Pos: loc.Pos.Move(loc.Dir, 1), Dir: loc.Dir}
new_cost := cost + 1
if board[newLoc.Pos] != "#" {
if cost, ok := cost_so_far[newLoc]; !ok || cost >= new_cost {
cost_so_far[newLoc] = new_cost
new_path := duplicatePath(item_value.path)
new_path[newLoc.Pos] = true
new_item := utils.NewItem(StoredPath{
loc: newLoc,
path: new_path,
cost: new_cost,
}, new_cost+end.Manhattan(newLoc.Pos), 0)
heap.Push(&pq, new_item)
pq.Update(new_item, new_cost)
}
}
// Try to turn left
newLoc = utils.Location{Pos: loc.Pos, Dir: loc.Dir.Turn(utils.Left)}
new_cost = cost + 1000
if cost, ok := cost_so_far[newLoc]; !ok || cost >= new_cost {
cost_so_far[newLoc] = new_cost
new_path := duplicatePath(item_value.path)
new_path[newLoc.Pos] = true
new_item := utils.NewItem(StoredPath{
loc: newLoc,
path: new_path,
cost: new_cost,
}, new_cost+end.Manhattan(newLoc.Pos), 0)
heap.Push(&pq, new_item)
pq.Update(new_item, new_cost)
}
// Try to turn right
newLoc = utils.Location{Pos: loc.Pos, Dir: loc.Dir.Turn(utils.Right)}
new_cost = cost + 1000
if cost, ok := cost_so_far[newLoc]; !ok || cost >= new_cost {
cost_so_far[newLoc] = new_cost
new_path := duplicatePath(item_value.path)
new_path[newLoc.Pos] = true
new_item := utils.NewItem(StoredPath{
loc: newLoc,
path: new_path,
cost: new_cost,
}, new_cost+end.Manhattan(newLoc.Pos), 0)
heap.Push(&pq, new_item)
pq.Update(new_item, new_cost)
}
}
on_finish := map[utils.Position]bool{}
for _, path := range finishing_paths {
for pos := range path.path {
on_finish[pos] = true
}
}
fmt.Println(len(on_finish))
}
func duplicatePath(path map[utils.Position]bool) map[utils.Position]bool {
new_path := make(map[utils.Position]bool)
for pos := range path {
new_path[pos] = true
}
return new_path
}
func main() {
input := utils.ReadInput("input.txt")
min := part1(input)
part2(input, min)
}