-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountdown_test.go
187 lines (173 loc) · 4.53 KB
/
countdown_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"fmt"
"log"
"math/rand"
"strconv"
"testing"
"github.com/cbehopkins/combination"
cntSlv "github.com/cbehopkins/countdown/cnt_slv"
)
type howwy struct {
Val int
How string
}
func newHowwy(val int, operator string, ha, hb string) howwy {
return howwy{val, "(" + ha + operator + hb + ")"}
}
func newHowwyArray(input []int) []howwy {
array := make([]howwy, len(input))
for i, v := range input {
array[i] = howwy{v, strconv.Itoa(v)}
}
return array
}
func do2(input []howwy) (result []howwy) {
if input[0].Val == 0 || input[1].Val == 0 {
return input
}
result = make([]howwy, 6)
a := input[0].Val
b := input[1].Val
aHow := input[0].How
bHow := input[1].How
result[0] = newHowwy(a+b, "+", aHow, bHow)
result[1] = newHowwy(a*b, "*", aHow, bHow)
eq := a == b
gt := a > b
amb0 := (a % b) == 0
bma0 := (b % a) == 0
if eq {
result[2] = newHowwy(0, "-", aHow, bHow)
result[3] = newHowwy(1, "/", aHow, bHow)
} else {
result[3] = newHowwy(0, "/", aHow, bHow) // technically not needed, but...
if gt {
result[2] = newHowwy(a-b, "-", aHow, bHow)
if amb0 {
result[3] = newHowwy(a/b, "/", aHow, bHow)
}
} else {
result[2] = newHowwy(b-a, "-", bHow, aHow)
if bma0 {
result[3] = newHowwy(b/a, "/", bHow, aHow)
}
}
}
result[4] = newHowwy(a, "", aHow, "")
result[5] = newHowwy(b, "", bHow, "")
return result
}
func select1(input []howwy) howwy {
nzFound := false
for _, v := range input {
if v.Val > 0 {
nzFound = true
}
}
if !nzFound {
log.Fatal("Zeroes only")
}
for true {
v := input[rand.Intn(len(input))]
if v.Val != 0 {
return v
}
}
return input[0]
}
func allN(input []howwy) howwy {
if len(input) == 1 {
return input[0]
}
if len(input) == 2 {
return select1(do2(input))
}
array := make([]howwy, len(input)-1)
array[0] = select1(do2(input[:2]))
copy(array[1:], input[2:])
return allN(array)
}
func singleHowwyTst(candidateArray []int, t *testing.T, repeat int) {
prevTarget := -1
for i := 0; i < repeat; i++ {
target := allN(newHowwyArray(candidateArray))
if target.Val == prevTarget {
i--
continue
}
prevTarget = target.Val
if !runArray(target.Val, candidateArray) {
t.Log("Candidate Array:", candidateArray)
t.Log("Target:", target)
t.Fatal("Unable to prove")
}
}
}
func runArray(target int, candidateArray []int) bool {
foundValues := cntSlv.NewNumMap() //pass it the proof list so it can auto-check for validity at the end
foundValues.SelfTest = true
foundValues.UseMult = true
tmpChan := foundValues.CountHelper(target, candidateArray)
for _ = range tmpChan {
}
if !foundValues.Solved() {
fmt.Println(foundValues.Numbers())
return false
}
return true
}
func TestAllCombinationsExhaustive(t *testing.T) {
t.Skip()
bigNumbersSet := []int{25, 50, 75, 100}
smallNumbersSet := []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10}
dstArrayInner := make([]int, 4)
dstArrayOuter := make([]int, 3)
dstArray := make([]int, 6)
copyFuncOuter := func(i, j int) {
dstArrayOuter[i] = bigNumbersSet[j]
}
copyFuncInner := func(i, j int) {
dstArrayInner[i] = smallNumbersSet[j]
}
cnt := 0
var i uint64
i = 0
for outerLength := 2; outerLength < 3; outerLength++ {
gcOuter := combination.NewGeneric(len(bigNumbersSet), outerLength, copyFuncOuter)
for err := gcOuter.Next(); err == nil; err = gcOuter.Next() {
gcInner := combination.NewGeneric(len(smallNumbersSet), 6-outerLength, copyFuncInner)
for err := gcInner.Next(); err == nil; err = gcInner.NextSkipN(1024 * 256) {
i++
copy(dstArray[:outerLength], dstArrayOuter)
copy(dstArray[outerLength:], dstArrayInner)
singleHowwyTst(dstArray, t, 2)
cnt++
if cnt >= 128 {
t.Log("Done 128", i, dstArray)
cnt = 0
}
}
break
}
}
}
func TestAllCombinationsRand(t *testing.T) {
bigNumbersSet := []int{25, 50, 75, 100}
smallNumbersSet := []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10}
combinedNumbersSet := append(bigNumbersSet, smallNumbersSet...)
dstArray := make([]int, 6)
copyFunc := func(i, j int) {
dstArray[i] = combinedNumbersSet[j]
}
todoChan := combination.RandomToDoSource((1 << 11), len(combinedNumbersSet), 6)
gcc := combination.NewGenericFromChan(todoChan, copyFunc)
i := 0
for err := gcc.Next(); err == nil; err = gcc.Next() {
i++
if i%16 == 0 {
fmt.Println(dstArray)
}
singleHowwyTst(dstArray, t, 16)
}
}