-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
121 lines (104 loc) · 2.65 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
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
package main
import (
"errors"
"flag"
"fmt"
"time"
)
func main() {
version := flag.Bool("version", false, "Get current program version")
directInput := flag.String("stringinput", "", "Load sudoku from terminal")
directOutput := flag.Bool("stringoutput", false, "Print solved sudoku in terminal")
fileInput := flag.String("fileinput", "", "Load sudoku from file")
fileOutput := flag.String("fileoutput", "", "Write solved sudoku in file")
imageInput := flag.String("imageinput", "", "Load sudoku from image")
imageOutput := flag.String("imageoutput", "", "Write solved sudoku over image")
plaintextOutput := flag.Bool("plaintext", false, "Print solved sudoku in plaintext")
flag.Parse()
if flag.NFlag() == 0 {
printError(errors.New("No flags provided"))
flag.PrintDefaults()
// now exit
return
}
// check version flag
if *version {
printSuccess("Current version:", CurrentVersion)
return
}
// check input flags
if *directInput == "" && *fileInput == "" && *imageInput == "" {
printError(errors.New("No output provided"))
fmt.Print("\n")
flag.PrintDefaults()
return
}
// check output flags
if !*directOutput && *fileOutput == "" && *imageOutput == "" {
printError(errors.New("No output provided"))
fmt.Print("\n")
flag.PrintDefaults()
return
}
// variables declaration
var e error
var s Sudoku
s = NewSudoku()
// check input flags
if *directInput != "" {
if e = s.LoadFromBytes([]byte(*directInput)); e != nil {
printError(e)
return
}
printSuccess("Sudoku loaded from terminal")
} else if *fileInput != "" {
if e := s.LoadFromFile(*fileInput); e != nil {
printError(e)
return
}
printSuccess("Sudoku loaded from file", *fileInput)
} else if *imageInput != "" {
if e := s.LoadFromImage(*imageInput); e != nil {
printError(e)
return
}
printSuccess("Sudoku loaded from image", *imageInput)
}
// start measuring time
started := time.Now()
// attempt to solve the sudoku
var iterations int64
iterations, e = s.Solve()
if e != nil {
printError(e)
return
}
// calculate elapsed time
elapsed := time.Now().Sub(started)
printSuccess("Solved in", iterations, "iterations")
printSuccess("It took", elapsed)
// save the output as wanted by the user
if *directOutput {
// print in terminal
fmt.Println()
fmt.Println(s.ShowGrid(*plaintextOutput))
}
if *fileOutput != "" {
// save to file
s.SaveToFile(*fileOutput)
if e != nil {
printError(e)
return
}
printSuccess("File", *fileOutput, "created")
}
if *imageOutput != "" {
// write as image
e = s.SaveToImage(*imageOutput)
if e != nil {
printError(e)
return
}
printSuccess("Image", *imageOutput, "created")
}
}