Skip to content

Commit aa65eb2

Browse files
committed
2024-day13
1 parent a8809b7 commit aa65eb2

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

2024/day13/.bench

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Lines":[{"Name":"Part 1","N":1888,"NsPerOp":573599,"AllocedBytesPerOp":0,"AllocsPerOp":0,"MBPerS":0,"Measured":1,"Ord":0},{"Name":"Part 2","N":2113,"NsPerOp":569922,"AllocedBytesPerOp":0,"AllocsPerOp":0,"MBPerS":0,"Measured":1,"Ord":0}],"Measured":1}

2024/day13/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- You can add some comments here if you want to :D -->

2024/day13/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"aocli/utils/reader"
5+
_ "embed"
6+
"fmt"
7+
"image"
8+
"os"
9+
)
10+
11+
//go:embed input.txt
12+
var input string
13+
14+
//go:embed input_test.txt
15+
var inputTest string
16+
17+
type machine struct {
18+
A, B, P image.Point
19+
}
20+
21+
func main() {
22+
// Check argv if we use test input or not
23+
if len(os.Args) > 1 && os.Args[1] == "test" {
24+
input = inputTest
25+
}
26+
27+
answer := solve(input, 0)
28+
println(answer)
29+
30+
answer = solve(input, 10000000000000)
31+
println(answer)
32+
}
33+
34+
func solve(input string, n int) int {
35+
lines := reader.FileDoubleLine(input)
36+
var machines []machine
37+
for _, line := range lines {
38+
var A, B, P image.Point
39+
fmt.Sscanf(line, "Button A: X+%d, Y+%d\nButton B: X+%d, Y+%d\nPrize: X=%d, Y=%d\n", &A.X, &A.Y, &B.X, &B.Y, &P.X, &P.Y)
40+
machines = append(machines, machine{A, B, P.Add(image.Point{n, n})})
41+
}
42+
43+
var res int
44+
45+
for _, m := range machines {
46+
res += calculate(m)
47+
}
48+
return res
49+
}
50+
51+
func calculate(m machine) int {
52+
aP := (m.B.Y*m.P.X - m.B.X*m.P.Y) / (m.A.X*m.B.Y - m.A.Y*m.B.X)
53+
bP := (m.A.Y*m.P.X - m.A.X*m.P.Y) / (m.A.Y*m.B.X - m.A.X*m.B.Y)
54+
55+
if m.A.Mul(aP).Add(m.B.Mul(bP)) == m.P {
56+
return aP*3 + bP
57+
}
58+
return 0
59+
}

2024/day13/main_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "testing"
4+
5+
func BenchmarkPartOne(b *testing.B) {
6+
for n := 0; n < b.N; n++ {
7+
solve(input, 0)
8+
}
9+
}
10+
11+
func BenchmarkPartTwo(b *testing.B) {
12+
for n := 0; n < b.N; n++ {
13+
solve(input, 10000000000000)
14+
}
15+
}

0 commit comments

Comments
 (0)