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