2021 day23, its slow... but it works :) lots of small bugs that got hunted via tests

This commit is contained in:
alexchao26
2021-12-25 22:34:04 -05:00
parent cd5d4b0a78
commit dd88e03f2c
3 changed files with 265 additions and 183 deletions
+168 -67
View File
@@ -2,7 +2,9 @@ package main
import (
_ "embed"
"fmt"
"reflect"
"strings"
"testing"
)
@@ -18,96 +20,94 @@ var doneInput = `#############
#A#B#C#D#
######### `
func Test_part1(t *testing.T) {
func Test_amphipodDay23(t *testing.T) {
tests := []struct {
name string
input string
part int
want int
}{
{
name: "example",
name: "part1 example",
input: example,
part: 1,
want: 12521,
},
{
name: "simple",
name: "part1 simple",
input: `#############
#.A.........#
###.#B#C#D###
#A#B#C#D#
#A#B#C#D#
######### `,
part: 1,
want: 2,
},
{
name: "simple: A then B",
name: "part1 simple: A then B",
input: `#############
#BA.........#
###.#.#C#D###
#A#B#C#D#
#A#B#C#D#
######### `,
part: 1,
want: 52,
},
{
// NOTE found a bug! A moving from a deep room to another room is calculating energy
// NOTE as if it is walking through the wall
name: "reversed B room", // B has to get out of A's way first
name: "part1 reversed B room", // B has to get out of A's way first
input: `#############
#.B.........#
###.#B#C#D###
#A#A#C#D#
#A#A#C#D#
######### `,
part: 1,
want: 95,
},
{
name: "some shuffling",
name: "part1 some shuffling",
input: `#############
#...........#
###A#B#C#D###
#A#C#B#D#
#A#C#B#D#
######### `,
part: 1,
want: 1120,
},
{
name: "doneInput",
name: "part1 doneInput",
input: doneInput,
part: 1,
want: 0,
},
{
name: "actual",
name: "part1 actual",
input: input,
part: 1,
want: 15299,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := part1(tt.input); got != tt.want {
t.Errorf("part1() = %v, want %v", got, tt.want)
}
})
}
}
func Test_part2(t *testing.T) {
tests := []struct {
name string
input string
want int
}{
{
name: "example",
name: "example part 2",
input: example,
part: 2,
want: 44169,
},
// {
// name: "actual",
// input: input,
// want: 0,
// },
{
name: "part2 actual",
input: input,
part: 2,
want: 47193,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := part2(tt.input); got != tt.want {
t.Errorf("part2() = %v, want %v", got, tt.want)
if testing.Short() && strings.Contains(tt.name, "actual") {
t.Skip(fmt.Sprintf("skipping %q in -short mode", tt.name))
}
if got := amphipodDay23(tt.input, tt.part); got != tt.want {
t.Errorf("amphipodDay23() = %v, want %v", got, tt.want)
}
})
}
@@ -115,14 +115,16 @@ func Test_part2(t *testing.T) {
func Test_state_getUnsettledCoords(t *testing.T) {
tests := []struct {
name string
input string
want [][2]int
name string
input string
roomCoordToWantChar map[[2]int]string
want [][2]int
}{
{
name: "already done",
input: doneInput,
want: nil,
name: "already done",
input: doneInput,
roomCoordToWantChar: roomCoordToWantCharPart1,
want: nil,
},
{
name: "example - 2 \"done\" amphipods",
@@ -134,22 +136,41 @@ func Test_state_getUnsettledCoords(t *testing.T) {
#A#D#C#A#
#########
*/
want: [][2]int{{2, 3}, {2, 5}, {2, 7}, {2, 9}, {3, 5}, {3, 9}},
roomCoordToWantChar: roomCoordToWantCharPart1,
want: [][2]int{{2, 3}, {3, 5}, {2, 5}, {2, 7}, {3, 9}, {2, 9}},
},
{
name: "four unsettled coords",
input: ` #############
input: `#############
#AB.....D..D#
###.#.#C#.###
#A#B#C#.#
######### `,
want: [][2]int{{1, 1}, {1, 2}, {1, 8}, {1, 11}},
######### `,
roomCoordToWantChar: roomCoordToWantCharPart1,
want: [][2]int{{1, 1}, {1, 2}, {1, 8}, {1, 11}},
},
{
name: "part2 test",
input: `#############
#AB.....D..D#
###.#.#C#.###
#A#A#C#.#
#B#B#C#D#
#A#B#C#D#
######### `,
roomCoordToWantChar: roomCoordToWantCharPart2,
want: [][2]int{
// hallway
{1, 1}, {1, 2}, {1, 8}, {1, 11},
{4, 3}, {3, 3}, // A room
{3, 5}, // B room
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := parseInput(tt.input)
if got := s.getUnsettledCoords(); !reflect.DeepEqual(got, tt.want) {
if got := s.getUnsettledCoords(tt.roomCoordToWantChar); !reflect.DeepEqual(got, tt.want) {
t.Errorf("state.getUnsettledCoords() = %v, want %v", got, tt.want)
}
})
@@ -158,10 +179,11 @@ func Test_state_getUnsettledCoords(t *testing.T) {
func Test_state_getNextPossibleMoves(t *testing.T) {
tests := []struct {
name string
input string
unsettledCoord [2]int
want [][2]int
name string
input string
unsettledCoord [2]int
roomCoordToWantChar map[[2]int]string
want [][2]int
}{
{
name: "example, deep room is stuck",
@@ -173,46 +195,116 @@ func Test_state_getNextPossibleMoves(t *testing.T) {
#A#D#C#A#
#########
*/
unsettledCoord: [2]int{3, 3}, // DEEP room in A slot
want: nil,
unsettledCoord: [2]int{3, 3}, // DEEP room in A slot
roomCoordToWantChar: roomCoordToWantCharPart1,
want: nil,
},
{
name: "example-frontA",
input: example,
unsettledCoord: [2]int{2, 3}, // FRONT room in A slot
want: [][2]int{{1, 2}, {1, 4}, {1, 1}, {1, 6}, {1, 8}, {1, 10}, {1, 11}},
name: "example-frontA",
input: example,
unsettledCoord: [2]int{2, 3}, // FRONT room in A slot
roomCoordToWantChar: roomCoordToWantCharPart1,
want: [][2]int{{1, 2}, {1, 4}, {1, 1}, {1, 6}, {1, 8}, {1, 10}, {1, 11}},
},
{
name: "example-deepA-should be stuck",
input: example,
unsettledCoord: [2]int{3, 3}, // FRONT room in A slot
want: nil,
name: "example-deepA-should be stuck",
input: example,
unsettledCoord: [2]int{3, 3}, // FRONT room in A slot
roomCoordToWantChar: roomCoordToWantCharPart1,
want: nil,
},
{
name: "hallways to rooms ONLY",
input: ` #############
input: `#############
#AB.....D..D#
###.#.#C#.###
#A#B#C#.#
######### `,
unsettledCoord: [2]int{1, 2},
want: [][2]int{{2, 5}},
unsettledCoord: [2]int{1, 2},
roomCoordToWantChar: roomCoordToWantCharPart1,
want: [][2]int{{2, 5}},
},
{
name: "hallway to DEEP room",
input: ` #############
input: `#############
#AB.....D..D#
###.#.#C#.###
#A#B#C#.#
######### `,
unsettledCoord: [2]int{1, 8},
want: [][2]int{{3, 9}},
unsettledCoord: [2]int{1, 8},
roomCoordToWantChar: roomCoordToWantCharPart1,
want: [][2]int{{3, 9}},
},
{
name: "part2 simple",
input: `#############
#B..........#
###A#.#C#D###
#A#B#C#D#
#A#B#C#D#
#A#B#C#D#
######### `,
unsettledCoord: [2]int{1, 1},
roomCoordToWantChar: roomCoordToWantCharPart2,
want: [][2]int{{2, 5}},
},
{
name: "part2 back of room",
input: `#############
#B......B.BB#
###A#.#C#D###
#A#.#C#D#
#A#.#C#D#
#A#.#C#D#
######### `,
unsettledCoord: [2]int{1, 1},
roomCoordToWantChar: roomCoordToWantCharPart2,
want: [][2]int{{5, 5}},
},
{
name: "part2 back of room",
input: `#############
#B......B..B#
###A#.#C#D###
#A#.#C#D#
#A#.#C#D#
#A#B#C#D#
######### `,
unsettledCoord: [2]int{1, 8},
roomCoordToWantChar: roomCoordToWantCharPart2,
want: [][2]int{{4, 5}},
},
{
name: "part2 bug moving C from B room to C room",
input: `#############
#AA.....B.BD#
###B#.#.#.###
#D#C#.#.#
#D#B#C#C#
#A#D#C#A#
######### `,
unsettledCoord: [2]int{3, 5},
roomCoordToWantChar: roomCoordToWantCharPart2,
want: [][2]int{{1, 4}, {1, 6}, {3, 7}},
},
{
name: "part2 bug moving B out of B room bc of a blocked D",
input: `#############
#AA.....B.BD#
###B#.#.#.###
#D#.#C#.#
#D#B#C#C#
#A#D#C#A#
######### `,
unsettledCoord: [2]int{4, 5},
roomCoordToWantChar: roomCoordToWantCharPart2,
want: [][2]int{{1, 4}, {1, 6}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := parseInput(tt.input)
if got := s.getNextPossibleMoves(tt.unsettledCoord); !reflect.DeepEqual(got, tt.want) {
if got := s.getNextPossibleMoves(tt.unsettledCoord, tt.roomCoordToWantChar); !reflect.DeepEqual(got, tt.want) {
t.Errorf("state.getNextPossibleMoves() = %v, want %v", got, tt.want)
}
})
@@ -266,6 +358,15 @@ func Test_calcEnergy(t *testing.T) {
},
want: 600,
},
{
name: "part2 C",
args: args{
char: "C",
start: [2]int{5, 11},
end: [2]int{3, 7},
},
want: 1000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {