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

Commit 4bf1858

Browse files
committed
Refactor day13 solution to use int64
1 parent d1a0d89 commit 4bf1858

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

solutions/day13/main.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"fmt"
55
"github.com/terminalnode/adventofcode2024/common"
6-
"github.com/terminalnode/adventofcode2024/common/util"
76
"log"
87
"math"
98
)
@@ -20,7 +19,7 @@ func part1(
2019
return fmt.Sprintf("Failed to parse input: %v", err)
2120
}
2221

23-
sum := 0
22+
sum := int64(0)
2423
for _, p := range problems {
2524
minTokenCost := findMinTokenCost(p)
2625
if minTokenCost != -1 {
@@ -33,45 +32,42 @@ func part1(
3332

3433
func findMinTokenCost(
3534
p problem,
36-
) int {
37-
smallest := math.MaxInt
35+
) int64 {
36+
smallest := int64(math.MaxInt64)
3837

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 {
38+
for timesA := int64(0); true; timesA++ {
39+
cX := timesA * p.aX
40+
cY := timesA * p.aY
41+
if cX > p.goalX || cY > p.goalY {
4542
break
4643
}
4744

48-
minMoves := findMinTokenCostAfterA(c, timesA, p)
45+
minMoves := findMinTokenCostAfterA(cX, cY, timesA, p)
4946
if minMoves != -1 && minMoves < smallest {
5047
log.Printf("New smallest! %d", minMoves)
5148
smallest = minMoves
5249
}
5350
}
5451

55-
if smallest == math.MaxInt {
52+
if smallest == math.MaxInt64 {
5653
return -1
5754
}
5855
return smallest
5956
}
6057

6158
func findMinTokenCostAfterA(
62-
c util.Coordinate,
63-
timesA int,
59+
cX int64,
60+
cY int64,
61+
timesA int64,
6462
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-
}
63+
) int64 {
64+
for timesB := int64(0); true; timesB++ {
65+
newX := timesB*p.bX + cX
66+
newY := timesB*p.bY + cY
7167

72-
if newC.X > p.goal.X || newC.Y > p.goal.Y {
68+
if newX > p.goalX || newY > p.goalY {
7369
break
74-
} else if newC.X == p.goal.X && newC.Y == p.goal.Y {
70+
} else if newX == p.goalX && newY == p.goalY {
7571
return timesA*3 + timesB
7672
}
7773
}

solutions/day13/parse.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ package main
33
import (
44
"errors"
55
"fmt"
6-
"github.com/terminalnode/adventofcode2024/common/util"
76
"regexp"
87
"strconv"
98
)
109

1110
type problem struct {
12-
a util.Coordinate
13-
b util.Coordinate
14-
goal util.Coordinate
11+
aX int64
12+
aY int64
13+
bX int64
14+
bY int64
15+
goalX int64
16+
goalY int64
1517
}
1618

1719
var r = regexp.MustCompile(`Button A: X\+(\d+), Y\+(\d+)\nButton B: X\+(\d+), Y\+(\d+)\nPrize: X=(\d+), Y=(\d+)`)
@@ -31,19 +33,22 @@ func parseProblems(
3133
return problems, fmt.Errorf("expected match to be at least 7 long, but was %d", len(match))
3234
}
3335

34-
digits := make([]int, 6)
36+
digits := make([]int64, 6)
3537
for j := 0; j < 6; j++ {
36-
d, err := strconv.ParseInt(string(match[j+1]), 10, 0)
38+
d, err := strconv.ParseInt(string(match[j+1]), 10, 64)
3739
if err != nil {
3840
return problems, err
3941
}
40-
digits[j] = int(d)
42+
digits[j] = d
4143
}
4244

4345
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]},
46+
aX: digits[0],
47+
aY: digits[1],
48+
bX: digits[2],
49+
bY: digits[3],
50+
goalX: digits[4],
51+
goalY: digits[5],
4752
}
4853
}
4954

0 commit comments

Comments
 (0)