Advent of Code Solutions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
1.8 KiB

package adventoc2024
import (
"fmt"
"io"
"strings"
"testing"
)
func dup(input string) (io.Reader, io.Reader) {
return strings.NewReader(input), strings.NewReader(input)
}
type puzzle interface {
Part1(io.Reader) string
Part2(io.Reader) string
}
func TestPuzzles(t *testing.T) {
tests := []struct {
solver puzzle
ans1 string
ans2 string
inp1 string
inp2 string
}{
{
solver: Day1{},
ans1: "11",
ans2: "31",
inp1: `3 4
4 3
2 5
1 3
3 9
3 3`,
},
{
solver: Day2{},
ans1: "2",
ans2: "4",
inp1: `7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9`,
},
{
solver: Day3{},
ans1: "161",
ans2: "48",
inp1: "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))",
inp2: "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))",
},
{
solver: Day4{},
ans1: "18",
ans2: "9",
inp1: `MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX`,
inp2: `.M.S......
..A..MSMS.
.M.S.MAA..
..A.ASMSM.
.M.S.M....
..........
S.S.S.S.S.
.A.A.A.A..
M.M.M.M.M.
..........`,
},
{
solver: Day5{},
ans1: "143",
ans2: "123",
inp1: `47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47`,
},
}
for i, tt := range tests {
name := fmt.Sprintf("Day%d", i+1)
t.Run(name, func(t *testing.T) {
p1, p2 := dup(tt.inp1)
if tt.inp2 != "" {
p2 = strings.NewReader(tt.inp2)
}
if got := tt.solver.Part1(p1); got != tt.ans1 {
t.Errorf("Part 1 = %s; expected %s", got, tt.ans1)
}
if got := tt.solver.Part2(p2); got != tt.ans2 {
t.Errorf("Part 2 = %s; expected %s", got, tt.ans2)
}
})
}
}