Skip to content

Commit b4de75d

Browse files
committed
feat(angch/2024-13): Initial
1 parent 7a0d0d3 commit b4de75d

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

angch/2024-13/main.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"log"
7+
"os"
8+
"regexp"
9+
"runtime/pprof"
10+
"strconv"
11+
"time"
12+
)
13+
14+
func gcd(a, b int) int {
15+
for b != 0 {
16+
a, b = b, a%b
17+
}
18+
return a
19+
}
20+
21+
// Function to calculate the determinant of a 2x2 matrix
22+
func det(a, b, c, d int) int {
23+
return a*d - b*c
24+
}
25+
26+
func day13(file string) (part1, part2 int) {
27+
f, err := os.Open(file)
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
defer f.Close()
32+
scanner := bufio.NewScanner(f)
33+
regex := regexp.MustCompile(`Button (\w+): X\+([0-9]+), Y\+([0-9]+)`)
34+
regex2 := regexp.MustCompile(`Prize: X=([0-9]+), Y=([0-9]+)`)
35+
36+
for scanner.Scan() {
37+
l1 := scanner.Text()
38+
scanner.Scan()
39+
40+
l2 := scanner.Text()
41+
scanner.Scan()
42+
43+
l3 := scanner.Text()
44+
scanner.Scan()
45+
buttons := [][2]int{}
46+
b1, c1 := 0, 0
47+
48+
// Parse: Button A: X+31, Y+16
49+
50+
m := regex.FindStringSubmatch(l1)
51+
// log.Printf("%+v", m)
52+
b1, _ = strconv.Atoi(m[2])
53+
c1, _ = strconv.Atoi(m[3])
54+
55+
buttons = append(buttons, [2]int{b1, c1})
56+
57+
m = regex.FindStringSubmatch(l2)
58+
// a = m[1]
59+
b1, _ = strconv.Atoi(m[2])
60+
c1, _ = strconv.Atoi(m[3])
61+
buttons = append(buttons, [2]int{b1, c1})
62+
63+
m = regex2.FindStringSubmatch(l3)
64+
b1, _ = strconv.Atoi(m[1])
65+
c1, _ = strconv.Atoi(m[2])
66+
67+
prize := [2]int{b1, c1}
68+
69+
log.Println(buttons)
70+
log.Println("Prize", prize)
71+
72+
log.Println(gcd(buttons[0][0], prize[0]))
73+
buttona := buttons[0]
74+
buttonb := buttons[1]
75+
76+
// sum := 0
77+
x1, x2 := buttona[0], buttonb[0]
78+
y1, y2 := buttona[1], buttonb[1]
79+
x3, y3 := prize[0], prize[1]
80+
// Calculate the determinant of the coefficient matrix
81+
D := det(x1, x2, y1, y2)
82+
if D == 0 {
83+
fmt.Println("No unique solution exists.")
84+
} else {
85+
// Calculate the determinants for a and b
86+
Da := det(x3, x2, y3, y2)
87+
Db := det(x1, x3, y1, y3)
88+
// Solve for a and b using Cramer's rule
89+
a := Da / D
90+
b := Db / D
91+
92+
if a*x1+b*x2 == x3 && a*y1+b*y2 == y3 {
93+
fmt.Printf("The solution is: a = %d, b = %d\n", a, b)
94+
part1 += a*3 + b
95+
} else {
96+
fmt.Println("The solution is incorrect.")
97+
}
98+
}
99+
100+
x3, y3 = x3+10000000000000, y3+10000000000000
101+
// Calculate the determinants for a and b
102+
Da := det(x3, x2, y3, y2)
103+
Db := det(x1, x3, y1, y3)
104+
// Solve for a and b using Cramer's rule
105+
a := Da / D
106+
b := Db / D
107+
108+
if a*x1+b*x2 == x3 && a*y1+b*y2 == y3 {
109+
fmt.Printf("The solution is: a = %d, b = %d\n", a, b)
110+
part2 += a*3 + b
111+
} else {
112+
fmt.Println("The solution is incorrect.")
113+
}
114+
}
115+
116+
return
117+
}
118+
119+
func main() {
120+
log.SetFlags(log.Lshortfile | log.LstdFlags)
121+
logperf := false
122+
if logperf {
123+
pf, _ := os.Create("default.pgo")
124+
err := pprof.StartCPUProfile(pf)
125+
if err != nil {
126+
log.Fatal(err)
127+
}
128+
defer pf.Close()
129+
}
130+
t1 := time.Now()
131+
part1, part2 := day13("test.txt")
132+
fmt.Println(part1, part2)
133+
if part1 != 480 || part2 != 875318608908 {
134+
log.Fatal("Test failed ", part1, part2)
135+
}
136+
fmt.Println(day13("input.txt"))
137+
if logperf {
138+
pprof.StopCPUProfile()
139+
}
140+
141+
fmt.Println("Elapsed time:", time.Since(t1))
142+
}

angch/2024-13/test.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Button A: X+94, Y+34
2+
Button B: X+22, Y+67
3+
Prize: X=8400, Y=5400
4+
5+
Button A: X+26, Y+66
6+
Button B: X+67, Y+21
7+
Prize: X=12748, Y=12176
8+
9+
Button A: X+17, Y+86
10+
Button B: X+84, Y+37
11+
Prize: X=7870, Y=6450
12+
13+
Button A: X+69, Y+23
14+
Button B: X+27, Y+71
15+
Prize: X=18641, Y=10279

0 commit comments

Comments
 (0)