Day13
This commit is contained in:
76
2024/gareth/day13/day13.go
Normal file
76
2024/gareth/day13/day13.go
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package day13
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ClawMachine struct {
|
||||||
|
Ax, Ay int
|
||||||
|
Bx, By int
|
||||||
|
Px, Py int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part1(input string) int {
|
||||||
|
clawMachines := parseInput(input)
|
||||||
|
total := 0
|
||||||
|
for _, c := range clawMachines {
|
||||||
|
x, y := solveSimEquations(c.Ax, c.Bx, c.Px, c.Ay, c.By, c.Py)
|
||||||
|
total += x*3 + y
|
||||||
|
}
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part2(input string) int {
|
||||||
|
clawMachines := parseInput(input)
|
||||||
|
total := 0
|
||||||
|
for _, c := range clawMachines {
|
||||||
|
prizeX, prizeY := c.Px+10000000000000, c.Py+10000000000000
|
||||||
|
det, x, y := c.Ax*c.By-c.Bx*c.Ay, prizeX*c.By-c.Bx*prizeY, c.Ax*prizeY-prizeX*c.Ay
|
||||||
|
if det != 0 && x == (x/det)*det && y == (y/det)*det {
|
||||||
|
total += (x/det)*3 + (y / det)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseInput(input string) []ClawMachine {
|
||||||
|
clawMachines := strings.Split(strings.TrimSpace(input), "\n\n")
|
||||||
|
output := make([]ClawMachine, 0, len(clawMachines))
|
||||||
|
for _, c := range clawMachines {
|
||||||
|
var clawMachine ClawMachine
|
||||||
|
line := strings.Split(strings.TrimSpace(c), "\n")
|
||||||
|
|
||||||
|
re := regexp.MustCompile(`X\+(\d+), Y\+(\d+)`)
|
||||||
|
matches := re.FindStringSubmatch(line[0])
|
||||||
|
clawMachine.Ax, _ = strconv.Atoi(matches[1])
|
||||||
|
clawMachine.Ay, _ = strconv.Atoi(matches[2])
|
||||||
|
|
||||||
|
matches = re.FindStringSubmatch(line[1])
|
||||||
|
clawMachine.Bx, _ = strconv.Atoi(matches[1])
|
||||||
|
clawMachine.By, _ = strconv.Atoi(matches[2])
|
||||||
|
|
||||||
|
re = regexp.MustCompile(`X=(\d+), Y=(\d+)`)
|
||||||
|
matches = re.FindStringSubmatch(line[2])
|
||||||
|
clawMachine.Px, _ = strconv.Atoi(matches[1])
|
||||||
|
clawMachine.Py, _ = strconv.Atoi(matches[2])
|
||||||
|
|
||||||
|
output = append(output, clawMachine)
|
||||||
|
}
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
func solveSimEquations(a1, b1, c1, a2, b2, c2 int) (int, int) {
|
||||||
|
det := a1*b2 - a2*b1
|
||||||
|
if det == 0 || (c1*b2-c2*b1)%det != 0 || (a1*c2-a2*c1)%det != 0 {
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
x := (c1*b2 - c2*b1) / det
|
||||||
|
y := (a1*c2 - a2*c1) / det
|
||||||
|
if x < 0 || x > 100 || y < 0 || y > 100 {
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
return x, y
|
||||||
|
}
|
||||||
45
2024/gareth/day13/day13_test.go
Normal file
45
2024/gareth/day13/day13_test.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package day13
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
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`)
|
||||||
|
assert.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`)
|
||||||
|
assert.Equal(t, 0, r)
|
||||||
|
}
|
||||||
1279
2024/gareth/day13/input.txt
Normal file
1279
2024/gareth/day13/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"aoc2024/day11"
|
"aoc2024/day13"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@@ -9,9 +9,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
data, _ := os.ReadFile("day11/input.txt")
|
data, _ := os.ReadFile("day13/input.txt")
|
||||||
fmt.Printf("part 1: %d\n", day11.Part1(string(data)))
|
fmt.Printf("part 1: %d\n", day13.Part1(string(data)))
|
||||||
fmt.Printf("part 2: %d\n", day11.Part2(string(data)))
|
fmt.Printf("part 2: %d\n", day13.Part2(string(data)))
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
fmt.Printf("Execution time: %s\n", elapsed)
|
fmt.Printf("Execution time: %s\n", elapsed)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user