|
|
@ -1,68 +1,75 @@ |
|
|
|
package main |
|
|
|
|
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"strings" |
|
|
|
"testing" |
|
|
|
) |
|
|
|
|
|
|
|
func TestDay01(t *testing.T) { |
|
|
|
ans1 := "142" |
|
|
|
input1 := `1abc2 |
|
|
|
pqr3stu8vwx |
|
|
|
a1b2c3d4e5f |
|
|
|
treb7uchet` |
|
|
|
type puzzleTest struct { |
|
|
|
inp1 string |
|
|
|
ans1 string |
|
|
|
|
|
|
|
ans2 := "281" |
|
|
|
input2 := `two1nine |
|
|
|
eightwothree |
|
|
|
abcone2threexyz |
|
|
|
xtwone3four |
|
|
|
4nineeightseven2 |
|
|
|
zoneight234 |
|
|
|
7pqrstsixteen` |
|
|
|
inp2 string |
|
|
|
ans2 string |
|
|
|
} |
|
|
|
|
|
|
|
puzzle := day01{} |
|
|
|
func TestPuzzles(t *testing.T) { |
|
|
|
for i, puzzle := range puzzles { |
|
|
|
name := fmt.Sprintf("day %d", i+1) |
|
|
|
test := tests[i] |
|
|
|
t.Run(name, func(t *testing.T) { |
|
|
|
|
|
|
|
res1 := puzzle.solve1(strings.NewReader(input1)) |
|
|
|
t.Logf("day 1 solution 1: %s", res1) |
|
|
|
if res1 != ans1 { |
|
|
|
t.Errorf("day 1 solution 1 = %s; wanted %s", res1, ans1) |
|
|
|
if test.inp2 == "" { |
|
|
|
test.inp2 = test.inp1 |
|
|
|
} |
|
|
|
res1 := puzzle.solve1(strings.NewReader(test.inp1)) |
|
|
|
res2 := puzzle.solve2(strings.NewReader(test.inp2)) |
|
|
|
|
|
|
|
res2 := puzzle.solve2(strings.NewReader(input2)) |
|
|
|
t.Logf("day 1 solution 2: %s", res2) |
|
|
|
if res2 != ans2 { |
|
|
|
t.Errorf("day 1 solution 2 = %s; wanted %s", res2, ans2) |
|
|
|
} |
|
|
|
if res1 != "incomplete" && res1 != test.ans1 { |
|
|
|
t.Errorf("solve1() = %s; wanted %s", res1, test.ans1) |
|
|
|
} |
|
|
|
|
|
|
|
func TestDay02(t *testing.T) { |
|
|
|
input := `Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green |
|
|
|
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue |
|
|
|
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red |
|
|
|
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red |
|
|
|
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green` |
|
|
|
|
|
|
|
puzzle := day02{} |
|
|
|
|
|
|
|
ans1 := "8" |
|
|
|
res1 := puzzle.solve1(strings.NewReader(input)) |
|
|
|
t.Logf("day 2 solution 1: %s", res1) |
|
|
|
if res1 != ans1 { |
|
|
|
t.Errorf("day 2 solution 1 = %s; wanted %s", res1, ans1) |
|
|
|
if res2 != "incomplete" && res2 != test.ans2 { |
|
|
|
t.Errorf("solve2() = %s; wanted %s", res2, test.ans2) |
|
|
|
} |
|
|
|
|
|
|
|
ans2 := "2286" |
|
|
|
res2 := puzzle.solve2(strings.NewReader(input)) |
|
|
|
t.Logf("day 2 solution 2: %s", res2) |
|
|
|
if res2 != ans2 { |
|
|
|
t.Errorf("day 2 solution 2 = %s; wanted %s", res2, ans2) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func TestDay03(t *testing.T) { |
|
|
|
input1 := `467..114.. |
|
|
|
var ( |
|
|
|
tests = []puzzleTest{ |
|
|
|
// day 1
|
|
|
|
{ |
|
|
|
inp1: `1abc2 |
|
|
|
pqr3stu8vwx |
|
|
|
a1b2c3d4e5f |
|
|
|
treb7uchet`, |
|
|
|
ans1: "142", |
|
|
|
|
|
|
|
inp2: `two1nine |
|
|
|
eightwothree |
|
|
|
abcone2threexyz |
|
|
|
xtwone3four |
|
|
|
4nineeightseven2 |
|
|
|
zoneight234 |
|
|
|
7pqrstsixteen`, |
|
|
|
ans2: "281", |
|
|
|
}, |
|
|
|
// day 2
|
|
|
|
{ |
|
|
|
inp1: `Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green |
|
|
|
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue |
|
|
|
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red |
|
|
|
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red |
|
|
|
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green`, |
|
|
|
ans1: "8", |
|
|
|
ans2: "2286", |
|
|
|
}, |
|
|
|
// day 3
|
|
|
|
{ |
|
|
|
inp1: `467..114.. |
|
|
|
...*...... |
|
|
|
..35..633. |
|
|
|
......#... |
|
|
@ -71,42 +78,23 @@ func TestDay03(t *testing.T) { |
|
|
|
..592..... |
|
|
|
......755. |
|
|
|
...$.*.... |
|
|
|
.664.598..` |
|
|
|
|
|
|
|
puzzle := day03{} |
|
|
|
|
|
|
|
ans1 := "4361" |
|
|
|
res1 := puzzle.solve1(strings.NewReader(input1)) |
|
|
|
if res1 != ans1 { |
|
|
|
t.Errorf("day 3 solution 1 = %s; wanted %s", res1, ans1) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func TestDay04(t *testing.T) { |
|
|
|
input1 := `Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 |
|
|
|
.664.598..`, |
|
|
|
ans1: "4361", |
|
|
|
}, |
|
|
|
// day 4
|
|
|
|
{ |
|
|
|
inp1: `Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 |
|
|
|
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 |
|
|
|
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 |
|
|
|
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 |
|
|
|
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 |
|
|
|
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11` |
|
|
|
|
|
|
|
puzzle := day04{} |
|
|
|
ans1 := "13" |
|
|
|
res1 := puzzle.solve1(strings.NewReader(input1)) |
|
|
|
if res1 != ans1 { |
|
|
|
t.Errorf("solve1() = %s; wanted %s", res1, ans1) |
|
|
|
} |
|
|
|
|
|
|
|
ans2 := "30" |
|
|
|
res2 := puzzle.solve2(strings.NewReader(input1)) |
|
|
|
if res2 != ans2 { |
|
|
|
t.Errorf("solve2() = %s; wanted %s", res2, ans2) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func TestDay05(t *testing.T) { |
|
|
|
input := `seeds: 79 14 55 13 |
|
|
|
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11`, |
|
|
|
ans1: "13", |
|
|
|
ans2: "30", |
|
|
|
}, |
|
|
|
// day 5
|
|
|
|
{ |
|
|
|
inp1: `seeds: 79 14 55 13 |
|
|
|
|
|
|
|
seed-to-soil map: |
|
|
|
50 98 2 |
|
|
@ -138,61 +126,26 @@ temperature-to-humidity map: |
|
|
|
|
|
|
|
humidity-to-location map: |
|
|
|
60 56 37 |
|
|
|
56 93 4` |
|
|
|
|
|
|
|
puzzle := day05{} |
|
|
|
|
|
|
|
ans := "35" |
|
|
|
res := puzzle.solve1(strings.NewReader(input)) |
|
|
|
if ans != res { |
|
|
|
t.Errorf("solve1() = %s; wanted %s", res, ans) |
|
|
|
} |
|
|
|
|
|
|
|
// ans = "46"
|
|
|
|
// res = puzzle.solve2(strings.NewReader(input))
|
|
|
|
// if ans != res {
|
|
|
|
// t.Errorf("solve2() = %s; wanted %s", res, ans)
|
|
|
|
// }
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func TestDay06(t *testing.T) { |
|
|
|
input := `Time: 7 15 30 |
|
|
|
Distance: 9 40 200` |
|
|
|
|
|
|
|
puzzle := day06{} |
|
|
|
|
|
|
|
ans := "288" |
|
|
|
res := puzzle.solve1(strings.NewReader(input)) |
|
|
|
if ans != res { |
|
|
|
t.Errorf("solve1() = %s; wanted %s", res, ans) |
|
|
|
} |
|
|
|
|
|
|
|
ans = "71503" |
|
|
|
res = puzzle.solve2(strings.NewReader(input)) |
|
|
|
if ans != res { |
|
|
|
t.Errorf("solve2() = %s; wanted %s", res, ans) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func TestDay07(t *testing.T) { |
|
|
|
input := `32T3K 765 |
|
|
|
56 93 4`, |
|
|
|
ans1: "35", |
|
|
|
ans2: "46", |
|
|
|
}, |
|
|
|
// day 6
|
|
|
|
{ |
|
|
|
inp1: `Time: 7 15 30 |
|
|
|
Distance: 9 40 200`, |
|
|
|
ans1: "288", |
|
|
|
ans2: "71503", |
|
|
|
}, |
|
|
|
// day 7
|
|
|
|
{ |
|
|
|
inp1: `32T3K 765 |
|
|
|
T55J5 684 |
|
|
|
KK677 28 |
|
|
|
KTJJT 220 |
|
|
|
QQQJA 483` |
|
|
|
|
|
|
|
puzzle := day07{} |
|
|
|
|
|
|
|
ans := "6440" |
|
|
|
res := puzzle.solve1(strings.NewReader(input)) |
|
|
|
if ans != res { |
|
|
|
t.Errorf("solve1() = %s; wanted %s", res, ans) |
|
|
|
} |
|
|
|
|
|
|
|
ans = "5905" |
|
|
|
res = puzzle.solve2(strings.NewReader(input)) |
|
|
|
if ans != res { |
|
|
|
t.Errorf("solve2() = %s; wanted %s", res, ans) |
|
|
|
} |
|
|
|
QQQJA 483`, |
|
|
|
ans1: "6440", |
|
|
|
ans2: "5905", |
|
|
|
}, |
|
|
|
} |
|
|
|
) |
|
|
|