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
|
package main
import (
"bufio"
"container/heap"
"fmt"
"math"
"os"
"strconv"
"strings"
)
func main() {
// fmt.Println(part1(readData("example.txt", 0)), 480)
// fmt.Println(part1(readData("data.txt", 0)))
fmt.Println(part2(readData("example.txt", 0)), 480)
fmt.Println(part2(readData("data.txt", 0)), 31623)
fmt.Println(part2(readData("example.txt", 10000000000000)))
fmt.Println(part2(readData("data.txt", 10000000000000)))
}
type Machine struct {
PrizeX, PrizeY int64
DxA, DyA int64
DxB, DyB int64
}
type Position struct {
X, Y int64
}
type Candidate struct {
Pos Position
EstimateCost int64
}
type CandidateItem struct {
Cnd *Candidate
Index int
}
type CandidateQueue struct {
elements []*CandidateItem
index map[Position]*CandidateItem
}
func MakeCandidateQueue() *CandidateQueue {
queue := new(CandidateQueue)
queue.index = make(map[Position]*CandidateItem)
return queue
}
func (queue *CandidateQueue) Len() int {
return len(queue.elements)
}
func (queue *CandidateQueue) Less(i, j int) bool {
return queue.elements[i].Cnd.EstimateCost < queue.elements[i].Cnd.EstimateCost
}
func (queue *CandidateQueue) Swap(i, j int) {
queue.elements[i], queue.elements[j] = queue.elements[j], queue.elements[i]
queue.elements[i].Index = i
queue.elements[j].Index = j
}
func (queue *CandidateQueue) Push(x any) {
candidate := x.(Candidate)
item := &CandidateItem{&candidate, len(queue.elements)}
queue.elements = append(queue.elements, item)
queue.index[candidate.Pos] = item
}
func (queue *CandidateQueue) AddElement(candidate Candidate) {
item, alreadyIn := queue.index[candidate.Pos]
if alreadyIn {
item.Cnd = &candidate
heap.Fix(queue, item.Index)
} else {
heap.Push(queue, candidate)
}
}
func (queue *CandidateQueue) Pop() any {
if len(queue.elements) == 0 {
return nil
} else {
elmt := queue.elements[len(queue.elements)-1]
queue.elements = queue.elements[:len(queue.elements)-1]
delete(queue.index, elmt.Cnd.Pos)
return elmt.Cnd
}
}
func part1(machines []Machine) (totalPrice int64) {
for _, m := range machines {
sol := part1Machine(m)
if sol >= 0 {
totalPrice += sol
}
}
return
}
func part1Machine(machine Machine) (price int64) {
fmt.Println(machine)
maxStepX := max(machine.DxA, machine.DxB)
maxStepY := max(machine.DyA, machine.DyB)
estimateCost := func(x, y int64) int64 {
distX := machine.PrizeX - x
distY := machine.PrizeY - y
return int64(math.Floor(min(float64(distX)/float64(maxStepX), float64(distY)/float64(maxStepY))))
}
realCost := make([][]int64, machine.PrizeY+1)
for y := range machine.PrizeY + 1 {
realCost[y] = make([]int64, machine.PrizeX+1)
for x := range machine.PrizeX + 1 {
realCost[y][x] = math.MaxInt
}
}
realCost[0][0] = 0
queue := MakeCandidateQueue()
queue.AddElement(Candidate{Position{0, 0}, estimateCost(0, 0)})
for queue.Len() > 0 {
candidate := heap.Pop(queue).(*Candidate)
xa := candidate.Pos.X + machine.DxA
ya := candidate.Pos.Y + machine.DyA
realCostA := realCost[candidate.Pos.Y][candidate.Pos.X] + 3
if xa == machine.PrizeX && ya == machine.PrizeY {
return realCostA
} else if xa <= machine.PrizeX && ya <= machine.PrizeY && realCostA < realCost[ya][xa] {
realCost[ya][xa] = realCostA
queue.AddElement(Candidate{Position{xa, ya}, realCostA + estimateCost(xa, ya)})
}
xb := candidate.Pos.X + machine.DxB
yb := candidate.Pos.Y + machine.DyB
realCostB := realCost[candidate.Pos.Y][candidate.Pos.X] + 1
if xb == machine.PrizeX && yb == machine.PrizeY {
return realCostB
} else if xb <= machine.PrizeX && yb <= machine.PrizeY && realCostB < realCost[yb][xb] {
realCost[yb][xb] = realCostB
queue.AddElement(Candidate{Position{xb, yb}, realCostB + estimateCost(xb, yb)})
}
}
return -1
}
func part2(machines []Machine) (totalPrice int64) {
for _, m := range machines {
sol := part2Machine(m)
if sol >= 0 {
totalPrice += sol
}
}
return
}
func part2Machine(machine Machine) (price int64) {
// This is fucking dumb.
// The general problem uses annoying math with diophantine stuff and shit.
// But they constructed the dataset so that we can do the dumb thing of
// solving the equations like dumb fucks and it so happens that the real solutions
// minimize the cost metric. So fucking useless and stupid.
// ax a + bx b = tx
// ay a + by b = ty
//
// (ax bx) * (a) = (tx)
// (ay by) (b) (ty)
//
// (a) = ( by -bx) * (tx)
// (b) (-ay ax) (ty) / (ax by - ay bx)
det := machine.DxA*machine.DyB - machine.DyA*machine.DxB
if det == 0 {
return -1
}
a := int64(float64(machine.DyB*machine.PrizeX-machine.DxB*machine.PrizeY) / float64(det))
b := int64(float64(machine.DxA*machine.PrizeY-machine.DyA*machine.PrizeX) / float64(det))
if machine.DxA*a+machine.DxB*b == machine.PrizeX && machine.DyA*a+machine.DyB*b == machine.PrizeY {
return a*3 + b
} else {
return -1
}
}
func trimAll(strs []string) {
for i, s := range strs {
strs[i] = strings.TrimSpace(s)
}
}
func parseCoordinateValue(s string) (value int) {
value, _ = strconv.Atoi(s[2:])
return
}
func parsePair(s string) (x, y int64) {
parts := strings.Split(strings.Split(s, ":")[1], ",")
trimAll(parts)
x = int64(parseCoordinateValue(parts[0]))
y = int64(parseCoordinateValue(parts[1]))
return
}
func readData(fileName string, offset int64) (machines []Machine) {
fp, _ := os.Open(fileName)
scanner := bufio.NewScanner(fp)
var machine Machine
for line := 0; scanner.Scan(); line++ {
switch line % 4 {
case 0:
machine = Machine{}
machine.DxA, machine.DyA = parsePair(scanner.Text())
case 1:
machine.DxB, machine.DyB = parsePair(scanner.Text())
case 2:
machine.PrizeX, machine.PrizeY = parsePair(scanner.Text())
machine.PrizeX += offset
machine.PrizeY += offset
machines = append(machines, machine)
}
}
return
}
|