updates
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
|
||||
}
|
||||
Reference in New Issue
Block a user