Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit d1a0d89

Browse files
committed
Add solution day 13 part 1
1 parent 2c31ae7 commit d1a0d89

File tree

4 files changed

+125
-3
lines changed

4 files changed

+125
-3
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ COPY common common
1414

1515
ARG DAY
1616
COPY solutions/day${DAY} solutions/day${DAY}
17-
RUN go build -o app solutions/day${DAY}/main.go
17+
RUN go build -o app solutions/day${DAY}/*
1818
EXPOSE 3000
1919

2020
HEALTHCHECK \

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ automatically rebuilt and redeployed ever time the `common` module or their own
5757
| 10 | ⭐ ⭐ | 23 | |
5858
| 11 | ⭐ ⭐ | 24 | |
5959
| 12 | ⭐ ⭐ | 25 | |
60-
| 13 | | | |
60+
| 13 | | | |

solutions/day13/main.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,80 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/terminalnode/adventofcode2024/common"
6+
"github.com/terminalnode/adventofcode2024/common/util"
7+
"log"
8+
"math"
59
)
610

711
func main() {
8-
common.Setup(13, nil, nil)
12+
common.Setup(13, part1, nil)
13+
}
14+
15+
func part1(
16+
input string,
17+
) string {
18+
problems, err := parseProblems(input)
19+
if err != nil {
20+
return fmt.Sprintf("Failed to parse input: %v", err)
21+
}
22+
23+
sum := 0
24+
for _, p := range problems {
25+
minTokenCost := findMinTokenCost(p)
26+
if minTokenCost != -1 {
27+
sum += minTokenCost
28+
}
29+
}
30+
31+
return fmt.Sprintf("At least %d tokens to solve these %d claw machines", sum, len(problems))
32+
}
33+
34+
func findMinTokenCost(
35+
p problem,
36+
) int {
37+
smallest := math.MaxInt
38+
39+
for timesA := 0; true; timesA++ {
40+
c := util.Coordinate{
41+
X: timesA * p.a.X,
42+
Y: timesA * p.a.Y,
43+
}
44+
if c.X > p.goal.X || c.Y > p.goal.Y {
45+
break
46+
}
47+
48+
minMoves := findMinTokenCostAfterA(c, timesA, p)
49+
if minMoves != -1 && minMoves < smallest {
50+
log.Printf("New smallest! %d", minMoves)
51+
smallest = minMoves
52+
}
53+
}
54+
55+
if smallest == math.MaxInt {
56+
return -1
57+
}
58+
return smallest
59+
}
60+
61+
func findMinTokenCostAfterA(
62+
c util.Coordinate,
63+
timesA int,
64+
p problem,
65+
) int {
66+
for timesB := 0; true; timesB++ {
67+
newC := util.Coordinate{
68+
X: timesB*p.b.X + c.X,
69+
Y: timesB*p.b.Y + c.Y,
70+
}
71+
72+
if newC.X > p.goal.X || newC.Y > p.goal.Y {
73+
break
74+
} else if newC.X == p.goal.X && newC.Y == p.goal.Y {
75+
return timesA*3 + timesB
76+
}
77+
}
78+
79+
return -1
980
}

solutions/day13/parse.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/terminalnode/adventofcode2024/common/util"
7+
"regexp"
8+
"strconv"
9+
)
10+
11+
type problem struct {
12+
a util.Coordinate
13+
b util.Coordinate
14+
goal util.Coordinate
15+
}
16+
17+
var r = regexp.MustCompile(`Button A: X\+(\d+), Y\+(\d+)\nButton B: X\+(\d+), Y\+(\d+)\nPrize: X=(\d+), Y=(\d+)`)
18+
19+
func parseProblems(
20+
input string,
21+
) ([]problem, error) {
22+
matches := r.FindAllSubmatch([]byte(input), -1)
23+
problems := make([]problem, len(matches))
24+
25+
if len(problems) == 0 {
26+
return problems, errors.New("input set contains no problems")
27+
}
28+
29+
for i, match := range matches {
30+
if len(match) < 7 {
31+
return problems, fmt.Errorf("expected match to be at least 7 long, but was %d", len(match))
32+
}
33+
34+
digits := make([]int, 6)
35+
for j := 0; j < 6; j++ {
36+
d, err := strconv.ParseInt(string(match[j+1]), 10, 0)
37+
if err != nil {
38+
return problems, err
39+
}
40+
digits[j] = int(d)
41+
}
42+
43+
problems[i] = problem{
44+
a: util.Coordinate{X: digits[0], Y: digits[1]},
45+
b: util.Coordinate{X: digits[2], Y: digits[3]},
46+
goal: util.Coordinate{X: digits[4], Y: digits[5]},
47+
}
48+
}
49+
50+
return problems, nil
51+
}

0 commit comments

Comments
 (0)