fix missing
This commit is contained in:
58
2024/go/day13/day13.go
Normal file
58
2024/go/day13/day13.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package day13
|
||||
|
||||
import (
|
||||
"adventofcode2024/utils"
|
||||
_ "adventofcode2024/utils/grid2d"
|
||||
_ "adventofcode2024/utils/inputs"
|
||||
"regexp"
|
||||
_ "fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Part1(input string) int {
|
||||
val := 0
|
||||
machines := strings.Split(input, "\n\n")
|
||||
pattern := `Button A: X\+(\d+), Y\+(\d+)\sButton B: X\+(\d+), Y\+(\d+)\sPrize: X=(\d+), Y=(\d+)`
|
||||
re := regexp.MustCompile(pattern)
|
||||
for _, machine := range machines {
|
||||
matches := re.FindStringSubmatch(machine)
|
||||
a := utils.MustAtoi(matches[1])
|
||||
b := utils.MustAtoi(matches[2])
|
||||
c := utils.MustAtoi(matches[3])
|
||||
d := utils.MustAtoi(matches[4])
|
||||
X := utils.MustAtoi(matches[5])
|
||||
Y := utils.MustAtoi(matches[6])
|
||||
// fmt.Println(matches)
|
||||
B := (Y*a - X*b)/(a*d - c*b)
|
||||
A := (X - c*B)/a
|
||||
// fmt.Printf("A: %d B: %d\n", A, B)
|
||||
if (A*a + B*c == X && A*b + B*d == Y) {
|
||||
val += A*3 + B
|
||||
}
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
val := 0
|
||||
machines := strings.Split(input, "\n\n")
|
||||
pattern := `Button A: X\+(\d+), Y\+(\d+)\sButton B: X\+(\d+), Y\+(\d+)\sPrize: X=(\d+), Y=(\d+)`
|
||||
re := regexp.MustCompile(pattern)
|
||||
for _, machine := range machines {
|
||||
matches := re.FindStringSubmatch(machine)
|
||||
a := utils.MustAtoi(matches[1])
|
||||
b := utils.MustAtoi(matches[2])
|
||||
c := utils.MustAtoi(matches[3])
|
||||
d := utils.MustAtoi(matches[4])
|
||||
X := utils.MustAtoi(matches[5]) + 10000000000000
|
||||
Y := utils.MustAtoi(matches[6]) + 10000000000000
|
||||
// fmt.Println(matches)
|
||||
B := (Y*a - X*b)/(a*d - c*b)
|
||||
A := (X - c*B)/a
|
||||
// fmt.Printf("A: %d B: %d\n", A, B)
|
||||
if (A*a + B*c == X && A*b + B*d == Y) {
|
||||
val += A*3 + B
|
||||
}
|
||||
}
|
||||
return val
|
||||
}
|
||||
45
2024/go/day13/day13_test.go
Normal file
45
2024/go/day13/day13_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package day13
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1(`Button A: X+94, Y+34
|
||||
Button B: X+22, Y+67
|
||||
Prize: X=8400, Y=5400
|
||||
|
||||
Button A: X+26, Y+66
|
||||
Button B: X+67, Y+21
|
||||
Prize: X=12748, Y=12176
|
||||
|
||||
Button A: X+17, Y+86
|
||||
Button B: X+84, Y+37
|
||||
Prize: X=7870, Y=6450
|
||||
|
||||
Button A: X+69, Y+23
|
||||
Button B: X+27, Y+71
|
||||
Prize: X=18641, Y=10279`)
|
||||
require.Equal(t, 480, r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2(`Button A: X+94, Y+34
|
||||
Button B: X+22, Y+67
|
||||
Prize: X=8400, Y=5400
|
||||
|
||||
Button A: X+26, Y+66
|
||||
Button B: X+67, Y+21
|
||||
Prize: X=12748, Y=12176
|
||||
|
||||
Button A: X+17, Y+86
|
||||
Button B: X+84, Y+37
|
||||
Prize: X=7870, Y=6450
|
||||
|
||||
Button A: X+69, Y+23
|
||||
Button B: X+27, Y+71
|
||||
Prize: X=18641, Y=10279`)
|
||||
require.Equal(t, 480, r)
|
||||
}
|
||||
1279
2024/go/day13/input.txt
Normal file
1279
2024/go/day13/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user