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