Skip to content

Commit

Permalink
day13
Browse files Browse the repository at this point in the history
  • Loading branch information
gamersi committed Dec 13, 2024
1 parent 48f7ca3 commit 7d19897
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
41 changes: 41 additions & 0 deletions day13p1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package day13p1

import (
"io"
"regexp"
"strconv"

"aoc/utils"
)

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
numregex := regexp.MustCompile(`\d+`)

// create blocks - blocks are 3 lines (until \n)
blocks := make([]string, 0)
for i := 0; i < len(lines); i += 4 {
blocks = append(blocks, lines[i]+"\n"+lines[i+1]+"\n"+lines[i+2])
}

sum := 0

for _, block := range blocks {
resultsStr := numregex.FindAllString(block, -1)
// convert to int
results := make([]int, len(resultsStr))
for i, r := range resultsStr {
results[i], _ = strconv.Atoi(r)
}
ax, ay, bx, by, px, py := results[0], results[1], results[2], results[3], results[4], results[5]
// i got this by making an equation system
denom := ax*by - ay*bx
a := (px*by - py*bx) / denom
b := (ax*py - ay*px) / denom
if ax*a+bx*b == px && ay*a+by*b == py {
sum += a*3 + b
}
}

return sum
}
47 changes: 47 additions & 0 deletions day13p1/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package day13p1

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `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`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 480},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}
43 changes: 43 additions & 0 deletions day13p2/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package day13p2

import (
"io"
"regexp"
"strconv"

"aoc/utils"
)

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
numregex := regexp.MustCompile(`\d+`)

// create blocks - blocks are 3 lines (until \n)
blocks := make([]string, 0)
for i := 0; i < len(lines); i += 4 {
blocks = append(blocks, lines[i]+"\n"+lines[i+1]+"\n"+lines[i+2])
}

sum := 0

for _, block := range blocks {
resultsStr := numregex.FindAllString(block, -1)
// convert to int
results := make([]int, len(resultsStr))
for i, r := range resultsStr {
results[i], _ = strconv.Atoi(r)
}
ax, ay, bx, by, px, py := results[0], results[1], results[2], results[3], results[4], results[5]
px += 10000000000000
py += 10000000000000
// i got this by making an equation system
denom := ax*by - ay*bx
a := (px*by - py*bx) / denom
b := (ax*py - ay*px) / denom
if ax*a+bx*b == px && ay*a+by*b == py {
sum += a*3 + b
}
}

return sum
}
47 changes: 47 additions & 0 deletions day13p2/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package day13p2

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `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`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 875318608908},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}

0 comments on commit 7d19897

Please sign in to comment.