-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
122 lines (102 loc) · 2.58 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
package main
import (
"fmt"
"regexp"
"github.com/believer/aoc-2024/utils"
"github.com/believer/aoc-2024/utils/files"
)
// Needed some tips to get the solution for part 2 and got to
// revive my linear algebra knowledge from 10+ years ago. I really
// enjoyed matrix math back in the day so that was fun :D
//
// Had made an error in the determinant Ax/Ay zero check and used && instead of ||
// It worked for test input, but not real. Took me way too long to find...
func main() {
fmt.Println("Part 1: ", part1("input.txt"))
fmt.Println("Part 2: ", part2("input.txt"))
}
func part1(name string) int {
return solve(name, 0)
}
func part2(name string) int {
return solve(name, 10000000000000)
}
func getEquationValues(prize []string) (int, int, int, int, int, int) {
re := regexp.MustCompile(`\d+`)
Ax, Ay, Bx, By := 0, 0, 0, 0
prizeX, prizeY := 0, 0
for i, part := range prize {
matches := re.FindAllString(part, -1)
x, y := utils.MustIntFromString(matches[0]), utils.MustIntFromString(matches[1])
switch i {
case 0:
Ax, Ay = x, y
case 1:
Bx, By = x, y
case 2:
prizeX, prizeY = x, y
}
}
return Ax, Ay, Bx, By, prizeX, prizeY
}
// We can use linear algebra to solve this.
//
// ax + by = c1
// cx + dy = c2
//
// Which in matrix form looks like:
//
// [a1 b1] [x] [c1]
// [a2 b2] [y] = [c2]
//
// Using the first matrix (the coefficient matrix), A, we can
// calculate the determinant:
//
// det(A) = a1b2 - a2b1
//
// This is only solvable if the determinant is _not_ zero
// From this we can use Cramer's rule to calculate x and y.
//
// [c1 b1]
// [c2 b2] = Ax
//
// [a1 c1]
// [a2 c2] = Ay
//
// Calculate the determinants like for A and then divide by det(A)
// to get the solutions for x and y:
//
// x = det(Ax) / det(A)
// y = det(Ay) / det(A)
func solve(name string, offset int) int {
prizes := files.ReadParagraphs(name)
tokens := 0
for _, prize := range prizes {
a1, a2, b1, b2, c1, c2 := getEquationValues(prize)
// Add offset
c1 += offset
c2 += offset
// Calculate determinant of coefficient matrix
detA := a1*b2 - a2*b1
// If the determinant is zero, there are either no or infinite solutions
if detA == 0 {
continue
}
detAx := (c1*b2 - c2*b1)
detAy := (c2*a1 - c1*a2)
// Ensure that we have integer solutions
// since we can only make full button presss and no partial presses
if detAx%detA != 0 || detAy%detA != 0 {
continue
}
x := detAx / detA
y := detAy / detA
// Only positive solutions since we can't press
// the buttons negative amounts
if x < 0 || y < 0 {
continue
}
tokens += 3*x + y
}
return tokens
}