-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |