/
main.go
93 lines (82 loc) · 1.96 KB
/
main.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
package main
import (
"bufio"
"flag"
"io/ioutil"
"log"
"os"
"runtime"
"runtime/pprof"
"github.com/sonntuet1997/numberlink-sat/numberlink"
"github.com/sonntuet1997/numberlink-sat/solver"
)
var (
isCNFMode bool
//isSolveMode bool
//isManyMode bool
cpuProfile string
memProfile string
algorithm string
)
func init() {
flag.BoolVar(&isCNFMode, "cnf", false, "Generate CNF")
//flag.BoolVar(&isSolveMode, "solve", true, "Solve with SAT solver")
//flag.BoolVar(&isManyMode, "many", false, "Solve many one-line 9x9 sudoku w/ gophersat")
flag.StringVar(&algorithm, "algorithm", "normal", "Normal or Product algorithm")
flag.StringVar(&cpuProfile, "cpu-profile", "", "Write CPU profile to a file")
flag.StringVar(&memProfile, "mem-profile", "", "Write memory profile to a file")
flag.Parse()
}
func main() {
if cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
log.Fatal(err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
return
}
defer pprof.StopCPUProfile()
}
mode := "solve"
if isCNFMode {
mode = "cnf"
}
bytes, _ := ioutil.ReadAll(os.Stdin)
input := string(bytes)
solve(mode, algorithm, input)
if memProfile != "" {
f, err := os.Create(memProfile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
}
}(f) // error handling omitted for example
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
}
func solve(mode, algorithm, input string) {
board := numberlink.NewFromString(input)
if mode == "cnf" {
cnf := solver.GenerateCNFConstraints(board, algorithm)
writer := bufio.NewWriter(os.Stdout)
cnf.Print(writer)
err := writer.Flush()
if err != nil {
return
}
return
}
if mode == "solve" {
println("cadical")
solver.SolveWithCustomSolver(board, "cadical -q", algorithm)
}
board.Print(os.Stdout)
}