From 7d19897e97af38bfb708fa3d5a17e8aadbf116ea Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 13 Dec 2024 16:20:14 +0100 Subject: [PATCH] day13 --- day13p1/solution.go | 41 +++++++++++++++++++++++++++++++++++ day13p1/solution_test.go | 47 ++++++++++++++++++++++++++++++++++++++++ day13p2/solution.go | 43 ++++++++++++++++++++++++++++++++++++ day13p2/solution_test.go | 47 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 178 insertions(+) create mode 100644 day13p1/solution.go create mode 100644 day13p1/solution_test.go create mode 100644 day13p2/solution.go create mode 100644 day13p2/solution_test.go diff --git a/day13p1/solution.go b/day13p1/solution.go new file mode 100644 index 0000000..d195418 --- /dev/null +++ b/day13p1/solution.go @@ -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 +} diff --git a/day13p1/solution_test.go b/day13p1/solution_test.go new file mode 100644 index 0000000..0612809 --- /dev/null +++ b/day13p1/solution_test.go @@ -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) + } + } +} diff --git a/day13p2/solution.go b/day13p2/solution.go new file mode 100644 index 0000000..385640e --- /dev/null +++ b/day13p2/solution.go @@ -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 +} diff --git a/day13p2/solution_test.go b/day13p2/solution_test.go new file mode 100644 index 0000000..0e3fbf2 --- /dev/null +++ b/day13p2/solution_test.go @@ -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) + } + } +}