-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolution_test.go
90 lines (80 loc) · 2.39 KB
/
solution_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package watersort
import (
"testing"
)
// level105 is "Water Sort Puzzle"'s infamous 105th level.
var level105 = State{
Bottles: []Bottle{
{Colors: []Color{Blue, Blue, DarkGreen, DarkBlue}},
{Colors: []Color{Gray, Green, Pink, Purple}},
{Colors: []Color{Brown, Red, Purple, Orange}},
{Colors: []Color{Orange, Red, Pink, Orange}},
{Colors: []Color{DarkBlue, Yellow, Red, DarkGreen}},
{Colors: []Color{DarkGreen, Brown, DarkGreen, Yellow}},
{Colors: []Color{LightGreen, Red, Purple, Brown}},
{Colors: []Color{LightGreen, Pink, Purple, LightGreen}},
{Colors: []Color{DarkBlue, Blue, Gray, Green}},
{Colors: []Color{Green, Gray, Yellow, Brown}},
{Colors: []Color{DarkBlue, LightGreen, Yellow, Gray}},
{Colors: []Color{Orange, Pink, Blue, Green}},
{Colors: []Color{Empty, Empty, Empty, Empty}},
{Colors: []Color{Empty, Empty, Empty, Empty}},
},
}
const level105OptimalSolution = 41
func TestFindSolution(t *testing.T) {
cases := []struct {
name string
in State
want int
}{
{
name: "solve puzzle",
in: State{
Bottles: []Bottle{
{Colors: []Color{Pink, Yellow, Purple, Orange}},
{Colors: []Color{DarkGreen, Pink, Blue, Red}},
{Colors: []Color{DarkGreen, DarkBlue, DarkBlue, Red}},
{Colors: []Color{DarkBlue, Gray, Pink, Gray}},
{Colors: []Color{Blue, Purple, Blue, Purple}},
{Colors: []Color{Green, Red, DarkBlue, Orange}},
{Colors: []Color{Yellow, DarkGreen, Orange, Gray}},
{Colors: []Color{Orange, Green, Green, Gray}},
{Colors: []Color{Red, Yellow, DarkGreen, Blue}},
{Colors: []Color{Pink, Yellow, Green, Purple}},
{Colors: []Color{Empty, Empty, Empty, Empty}},
{Colors: []Color{Empty, Empty, Empty, Empty}},
},
},
want: 31,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if err := tc.in.sanityCheck(); err != nil {
t.Fatal(err)
}
steps, err := tc.in.Solve()
if err != nil {
t.Fatal(err)
}
if got := len(steps); got != tc.want {
t.Errorf("solution has %d steps, want %d", got, tc.want)
}
})
}
}
func BenchmarkFindSolution(b *testing.B) {
if err := level105.sanityCheck(); err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
steps, err := level105.Clone().Solve()
if err != nil {
b.Error(err)
}
if got, want := len(steps), level105OptimalSolution; got != want {
b.Errorf("got solution with %d steps, want %d", got, want)
}
}
}