-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (67 loc) · 1.54 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
package main
import (
"fmt"
"github.com/devkvlt/aoc"
)
type Machine struct{ xA, yA, xB, yB, x, y int }
var machines []Machine
func init() {
chunks := aoc.Chunks("input")
machines = make([]Machine, len(chunks))
for i, chunk := range chunks {
fmt.Sscanf(chunk[0], "Button A: X+%d, Y+%d", &machines[i].xA, &machines[i].yA)
fmt.Sscanf(chunk[1], "Button B: X+%d, Y+%d", &machines[i].xB, &machines[i].yB)
fmt.Sscanf(chunk[2], "Prize: X=%d, Y=%d", &machines[i].x, &machines[i].y)
}
}
// α = number of times we push A
// β = number of times we push B
// System to solve: (1): x = α*xA + β*xB
// (2): y = α*yA + β*yB
// (1) => α*xA = x - β*xB
// => α = (x - β*xB)/xA
// (2) => y = (x - β*xB)*yA/xA + β*yB
// => y*xA = (x - β*xB)*yA + β*yB*xA
// => y*xA = x*yA - β*xB*yA + β*yB*xA
// => y*xA - x*yA = β*(yB*xA - xB*yA)
//
// FINALLY: β = (y*xA - x*yA)/(yB*xA - xB*yA)
// α = (x - β*xB)/xA
// price = 3*α + β
func price(m Machine) int {
x := m.x
y := m.y
xA := m.xA
yA := m.yA
xB := m.xB
yB := m.yB
if (y*xA-x*yA)%(yB*xA-xB*yA) != 0 {
return 0
}
β := (y*xA - x*yA) / (yB*xA - xB*yA)
if (x-β*xB)%xA != 0 {
return 0
}
α := (x - β*xB) / xA
return 3*α + β
}
func part1() {
sum := 0
for _, m := range machines {
sum += price(m)
}
fmt.Println(sum)
}
func part2() {
sum := 0
for _, m := range machines {
m.x += 10000000000000
m.y += 10000000000000
sum += price(m)
}
fmt.Println(sum)
}
func main() {
part1() // 40069
part2() // 71493195288102
}