-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
255 lines (223 loc) · 6.24 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
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
package main
import (
"container/heap"
"fmt"
"strings"
"github.com/Alex-Waring/AoC/utils"
)
type State struct {
robot1_pos utils.Position
last_move string
numbers_entered string
}
type HState struct {
robot1_pos utils.Position
last_move string
numbers_entered string
instructions string
}
type Cache struct {
robot1_pos utils.Position
last_move string
numbers_entered string
}
func solve(input []string, pads int) {
defer utils.Timer("part1")()
keypad := utils.NewBoard()
keypad[utils.Position{Row: 0, Col: 0}] = "7"
keypad[utils.Position{Row: 0, Col: 1}] = "8"
keypad[utils.Position{Row: 0, Col: 2}] = "9"
keypad[utils.Position{Row: 1, Col: 0}] = "4"
keypad[utils.Position{Row: 1, Col: 1}] = "5"
keypad[utils.Position{Row: 1, Col: 2}] = "6"
keypad[utils.Position{Row: 2, Col: 0}] = "1"
keypad[utils.Position{Row: 2, Col: 1}] = "2"
keypad[utils.Position{Row: 2, Col: 2}] = "3"
keypad[utils.Position{Row: 3, Col: 0}] = "X"
keypad[utils.Position{Row: 3, Col: 1}] = "0"
keypad[utils.Position{Row: 3, Col: 2}] = "A"
keypad_lookup := map[string]utils.Position{}
for pos, key := range keypad {
keypad_lookup[key] = pos
}
controller := utils.NewBoard()
controller[utils.Position{Row: 0, Col: 0}] = "X"
controller[utils.Position{Row: 0, Col: 1}] = "^"
controller[utils.Position{Row: 0, Col: 2}] = "A"
controller[utils.Position{Row: 1, Col: 0}] = "<"
controller[utils.Position{Row: 1, Col: 1}] = "v"
controller[utils.Position{Row: 1, Col: 2}] = ">"
numeric_map := map[int]int{
0: 319,
1: 85,
2: 143,
3: 286,
4: 789,
}
total := 0
controller_lookup := map[string]utils.Position{}
for pos, key := range controller {
controller_lookup[key] = pos
}
for i, line := range input {
pq := make(utils.PriorityQueue, 1)
pq[0] = utils.NewItem(State{
robot1_pos: keypad_lookup["A"],
last_move: "A",
numbers_entered: "",
}, 0, 0)
heap.Init(&pq)
// The cache doesn't care about the instructions
seen := map[Cache]bool{}
var finalLength int
keys := []string{"^", "v", "<", ">", "A"}
for pq.Len() > 0 {
item := heap.Pop(&pq).(*utils.Item)
state := item.GetValue().(State)
robot1_pos := state.robot1_pos
last_move := state.last_move
numbers_entered := state.numbers_entered
// Check if we've won
if state.numbers_entered == line {
finalLength = item.GetPriority()
break
}
// Check if we're off track
if !strings.HasPrefix(line, numbers_entered) {
continue
}
// Check if we're over the gap
if num, ok := keypad[robot1_pos]; !ok || num == "X" {
continue
}
// Check if we've seen this state before
cache := Cache{
robot1_pos: robot1_pos,
last_move: last_move,
numbers_entered: numbers_entered,
}
if _, ok := seen[cache]; ok {
continue
}
seen[cache] = true
// Try and press the last controller
for _, key := range keys {
new_robot1_pos := robot1_pos
new_out := numbers_entered
new_robot1_pos, output := doKeypad(new_robot1_pos, key, keypad)
if output != "" {
new_out += output
}
cost_move := heuristic(key, last_move, pads, controller_lookup, controller) + item.GetPriority()
newItem := utils.NewItem(State{
robot1_pos: new_robot1_pos,
last_move: key,
numbers_entered: new_out,
}, cost_move, 0)
heap.Push(&pq, newItem)
pq.Update(newItem, cost_move)
}
}
total += numeric_map[i] * finalLength
}
fmt.Println(total)
}
var heuristicCache = map[string]int{}
// Layers is the number of robots we have to control
func heuristic(move string, prev_move string, layers int, keypad_lookup map[string]utils.Position, keypad utils.Board) int {
// Quick caching
key := fmt.Sprintf("%s%s%d", move, prev_move, layers)
if val, ok := heuristicCache[key]; ok {
return val
}
// We've moved the robot, so now return
if layers == 0 {
return 1
}
start_pos := keypad_lookup[prev_move]
pq := make(utils.PriorityQueue, 1)
pq[0] = utils.NewItem(HState{
robot1_pos: start_pos,
last_move: "A",
numbers_entered: "",
instructions: "",
}, 0, 0)
heap.Init(&pq)
cache := map[string]int{}
// Use a priority queue and return when we find the answer, IE get the right move
for pq.Len() > 0 {
item := heap.Pop(&pq).(*utils.Item)
state := item.GetValue().(HState)
cost := item.GetPriority()
top_pos := state.robot1_pos
last_move := state.last_move
output := state.numbers_entered
instructions := state.instructions
// Check we're in a valid position
if val, ok := keypad[top_pos]; !ok || val == "X" {
continue
}
// Check if we've solved it and got the move
if output == move {
heuristicCache[key] = cost
return cost
}
// Cache, when the position and last move are the same, we've seen this before
seen_key := fmt.Sprintf("%s%s", top_pos.String(), last_move)
if _, ok := cache[seen_key]; ok {
continue
}
cache[seen_key] = cost
for _, move := range []string{"^", "v", "<", ">", "A"} {
new_pos := top_pos
new_pos, new_output := doMovePad(new_pos, move, keypad)
new_instructions := instructions + move
cost_move := heuristic(move, last_move, layers-1, keypad_lookup, keypad)
new_cost := cost + cost_move
newItem := utils.NewItem(HState{
robot1_pos: new_pos,
last_move: move,
numbers_entered: output + new_output,
instructions: new_instructions,
}, new_cost, 0)
heap.Push(&pq, newItem)
pq.Update(newItem, new_cost)
}
}
panic("no solution")
}
func doKeypad(pos utils.Position, move string, keypad utils.Board) (utils.Position, string) {
switch move {
case "A":
return pos, keypad[pos]
case "v":
return pos.Move(utils.Down, 1), ""
case "^":
return pos.Move(utils.Up, 1), ""
case "<":
return pos.Move(utils.Left, 1), ""
case ">":
return pos.Move(utils.Right, 1), ""
}
panic("not handled")
}
func doMovePad(pos utils.Position, move string, movepad utils.Board) (utils.Position, string) {
switch move {
case "A":
return pos, movepad[pos]
case "v":
return pos.Move(utils.Down, 1), ""
case "^":
return pos.Move(utils.Up, 1), ""
case "<":
return pos.Move(utils.Left, 1), ""
case ">":
return pos.Move(utils.Right, 1), ""
}
panic("not handled")
}
func main() {
input := utils.ReadInput("input.txt")
solve(input, 2)
solve(input, 25)
}