author | Mikael Berthe <mikael@lilotux.net> |
Fri, 09 Sep 2016 23:28:58 +0200 | |
changeset 5 | 733df0275d78 |
parent 1 | 6a396d691a7d |
child 10 | 8dc05ff5dbe2 |
permissions | -rw-r--r-- |
0 | 1 |
package main |
2 |
||
3 |
import ( |
|
4 |
"fmt" |
|
5 |
"log" |
|
6 |
"os" |
|
7 |
"time" |
|
8 |
||
9 |
flag "github.com/docker/docker/pkg/mflag" |
|
10 |
||
11 |
"mikael/takuzu" |
|
12 |
) |
|
13 |
||
14 |
var verbosity int |
|
15 |
||
16 |
func newTakuzuGameBoard(size int, simple bool, jobs int, buildBoardTimeout, reduceBoardTimeout time.Duration, minRatio, maxRatio int) *takuzu.Takuzu { |
|
17 |
results := make(chan *takuzu.Takuzu) |
|
18 |
||
19 |
newTak := func(i int) { |
|
20 |
takuzu, err := takuzu.NewRandomTakuzu(size, simple, fmt.Sprintf("%v", i), |
|
21 |
buildBoardTimeout, reduceBoardTimeout, minRatio, maxRatio) |
|
22 |
||
23 |
if err == nil && takuzu != nil { |
|
24 |
results <- takuzu |
|
25 |
if verbosity > 0 && jobs > 1 { |
|
26 |
log.Printf("Worker #%d done.", i) |
|
27 |
} |
|
28 |
} else { |
|
29 |
results <- nil |
|
30 |
} |
|
31 |
} |
|
32 |
||
33 |
if jobs == 0 { |
|
34 |
return nil |
|
35 |
} |
|
36 |
for i := 0; i < jobs; i++ { |
|
37 |
go newTak(i) |
|
38 |
} |
|
39 |
tak := <-results |
|
40 |
return tak |
|
41 |
} |
|
42 |
||
43 |
func main() { |
|
44 |
vbl := flag.Uint([]string{"-vl"}, 0, "Verbosity Level") |
|
45 |
simple := flag.Bool([]string{"-simple"}, false, "Only look for trivial solutions") |
|
46 |
out := flag.Bool([]string{"-out"}, false, "Send solution string to output") |
|
1
6a396d691a7d
Change switch --game to --board
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
47 |
board := flag.String([]string{"-board"}, "", "Load board string") |
0 | 48 |
schrodLvl := flag.Uint([]string{"-x-sl"}, 0, "[Advanced] Schrödinger level") |
49 |
resolveTimeout := flag.Duration([]string{"-x-timeout"}, 0, "[Advanced] Resolution timeout") |
|
50 |
buildBoardTimeout := flag.Duration([]string{"-x-build-timeout"}, 5*time.Minute, "[Advanced] Build timeout per resolution") |
|
51 |
reduceBoardTimeout := flag.Duration([]string{"-x-reduce-timeout"}, 20*time.Minute, "[Advanced] Reduction timeout") |
|
52 |
buildMinRatio := flag.Uint([]string{"-x-new-min-ratio"}, 55, "[Advanced] Build empty cell ratio (40-60)") |
|
53 |
buildMaxRatio := flag.Uint([]string{"-x-new-max-ratio"}, 62, "[Advanced] Build empty cell ratio (50-99)") |
|
54 |
all := flag.Bool([]string{"-all"}, false, "Look for all possible solutions") |
|
55 |
reduce := flag.Bool([]string{"-reduce"}, false, "Try to reduce the number of digits") |
|
56 |
buildNewSize := flag.Uint([]string{"-new"}, 0, "Build a new takuzu board (with given size)") |
|
57 |
pdfFileName := flag.String([]string{"-to-pdf"}, "", "PDF output file name") |
|
58 |
workers := flag.Uint([]string{"-workers"}, 1, "Number of parallel workers (use with --new)") |
|
59 |
||
60 |
flag.Parse() |
|
61 |
||
62 |
verbosity = int(*vbl) |
|
63 |
takuzu.SetVerbosityLevel(verbosity) |
|
64 |
takuzu.SetSchrodingerLevel(*schrodLvl) |
|
65 |
||
66 |
var tak *takuzu.Takuzu |
|
67 |
||
1
6a396d691a7d
Change switch --game to --board
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
68 |
if *board != "" { |
0 | 69 |
var err error |
1
6a396d691a7d
Change switch --game to --board
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
70 |
tak, err = takuzu.NewFromString(*board) |
0 | 71 |
if tak == nil || err != nil { |
72 |
fmt.Fprintln(os.Stderr, "Error:", err) |
|
73 |
tak = nil |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
if *buildNewSize > 0 { |
|
78 |
if verbosity > 1 { |
|
79 |
log.Printf("buildBoardTimeout: %v", *buildBoardTimeout) |
|
80 |
log.Printf("reduceBoardTimeout: %v", *reduceBoardTimeout) |
|
81 |
log.Printf("Free cell min ratio: %v", *buildMinRatio) |
|
82 |
log.Printf("Free cell max ratio: %v", *buildMaxRatio) |
|
83 |
} |
|
84 |
tak = newTakuzuGameBoard(int(*buildNewSize), *simple, |
|
85 |
int(*workers), |
|
86 |
*buildBoardTimeout, *reduceBoardTimeout, |
|
87 |
int(*buildMinRatio), int(*buildMaxRatio)) |
|
88 |
} |
|
89 |
||
90 |
if tak == nil { |
|
91 |
fmt.Fprintln(os.Stderr, "Could not create takuzu board.") |
|
92 |
os.Exit(255) |
|
93 |
} |
|
94 |
||
95 |
tak.DumpBoard() |
|
96 |
fmt.Println() |
|
97 |
||
98 |
if *pdfFileName != "" { |
|
99 |
if err := tak2pdf(tak, *pdfFileName); err != nil { |
|
100 |
log.Println(err) |
|
101 |
os.Exit(1) |
|
102 |
} |
|
103 |
if *out { |
|
104 |
tak.DumpString() |
|
105 |
} |
|
106 |
os.Exit(0) |
|
107 |
} |
|
108 |
||
109 |
if *buildNewSize > 0 { |
|
110 |
if *out { |
|
111 |
tak.DumpString() |
|
112 |
} |
|
113 |
os.Exit(0) |
|
114 |
} |
|
115 |
||
116 |
if *reduce { |
|
117 |
if verbosity > 1 { |
|
118 |
log.Printf("buildBoardTimeout: %v", *buildBoardTimeout) |
|
119 |
log.Printf("reduceBoardTimeout: %v", *reduceBoardTimeout) |
|
120 |
} |
|
121 |
var err error |
|
122 |
if tak, err = tak.ReduceBoard(*simple, "0", *buildBoardTimeout, *reduceBoardTimeout); err != nil { |
|
123 |
log.Println(err) |
|
124 |
os.Exit(1) |
|
125 |
} |
|
126 |
||
127 |
tak.DumpBoard() |
|
128 |
fmt.Println() |
|
129 |
||
130 |
if *out { |
|
131 |
tak.DumpString() |
|
132 |
} |
|
133 |
||
134 |
os.Exit(0) |
|
135 |
} |
|
136 |
||
137 |
if *simple { |
|
138 |
full, err := tak.TrySolveTrivial() |
|
139 |
if err != nil { |
|
140 |
log.Println(err) |
|
141 |
os.Exit(1) |
|
142 |
} |
|
143 |
if !full { |
|
144 |
tak.DumpBoard() |
|
5
733df0275d78
Dump result board string even if the resolution fails (with --out)
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
145 |
fmt.Println() |
733df0275d78
Dump result board string even if the resolution fails (with --out)
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
146 |
if *out { |
733df0275d78
Dump result board string even if the resolution fails (with --out)
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
147 |
tak.DumpString() |
733df0275d78
Dump result board string even if the resolution fails (with --out)
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
148 |
} |
0 | 149 |
log.Println("The takuzu could not be completed using trivial methods.") |
150 |
os.Exit(2) |
|
151 |
} |
|
152 |
||
153 |
log.Println("The takuzu is correct and complete.") |
|
154 |
tak.DumpBoard() |
|
155 |
fmt.Println() |
|
156 |
||
157 |
if *out { |
|
158 |
tak.DumpString() |
|
159 |
} |
|
160 |
os.Exit(0) |
|
161 |
} |
|
162 |
||
163 |
var allSol *[]takuzu.Takuzu |
|
164 |
if *all { |
|
165 |
allSol = &[]takuzu.Takuzu{} |
|
166 |
} |
|
167 |
res, err := tak.TrySolveRecurse(allSol, *resolveTimeout) |
|
168 |
if err != nil && verbosity > 1 { |
|
169 |
// The last trivial resolution failed |
|
170 |
log.Println("Trivial resolution failed:", err) |
|
171 |
} |
|
172 |
||
173 |
// Ignoring res & err if a full search was requested |
|
174 |
if *all { |
|
175 |
log.Println(len(*allSol), "solution(s) found.") |
|
176 |
if len(*allSol) > 0 { |
|
177 |
for _, s := range *allSol { |
|
178 |
if *out { |
|
179 |
s.DumpString() |
|
180 |
} else { |
|
181 |
s.DumpBoard() |
|
182 |
fmt.Println() |
|
183 |
} |
|
184 |
} |
|
185 |
if len(*allSol) > 1 { |
|
186 |
os.Exit(3) |
|
187 |
} |
|
188 |
os.Exit(0) |
|
189 |
} |
|
190 |
fmt.Println("No solution found.") |
|
191 |
os.Exit(2) |
|
192 |
} |
|
193 |
||
194 |
if err != nil { |
|
195 |
log.Println(err) |
|
196 |
os.Exit(1) |
|
197 |
} |
|
198 |
if res != nil { |
|
199 |
res.DumpBoard() |
|
200 |
fmt.Println() |
|
201 |
||
202 |
if *out { |
|
203 |
res.DumpString() |
|
204 |
} |
|
205 |
os.Exit(0) |
|
206 |
} |
|
207 |
||
208 |
fmt.Println("No solution found.") |
|
209 |
os.Exit(2) |
|
210 |
} |