-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmain.go
More file actions
136 lines (126 loc) · 3.03 KB
/
main.go
File metadata and controls
136 lines (126 loc) · 3.03 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import "fmt"
import "flag"
import "io"
import "os"
import "runtime/pprof"
import "strings"
import "strconv"
import "bufio"
var (
colorsFlag = flag.Bool("colors", false, "Make the output more readable with colors")
tubesFlag = flag.Bool("tubes", false, "Draw lines between sources")
callsFlag = flag.Bool("calls", false, "Count number of recursive calls")
callsOnlyFlag = flag.Bool("calls-only", false, "Print only the culminative number of recursive calls")
profileFlag = flag.String("profile", "", "Write profiling data to file")
generateFlag = flag.String("generate", "", "Generate a puzzle of a certain size. Usage: --generate=5x5")
)
func main() {
flag.Parse()
// Generating
if *generateFlag != "" {
size := strings.Split(*generateFlag, "x")
if len(size) != 2 {
fmt.Fprintf(os.Stderr, "Error: Must have exactly two arguments to --generate\n")
os.Exit(1)
}
width, err1 := strconv.Atoi(size[0])
height, err2 := strconv.Atoi(size[1])
if err1 != nil || err2 != nil {
fmt.Fprintf(os.Stderr, "Error: Unable to parse arguments to --generate\n")
os.Exit(1)
}
pzzl, _, err := Generate(width, height)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
} else {
fmt.Println(len(pzzl[0]), len(pzzl))
for _, line := range pzzl {
fmt.Println(line)
}
}
return
}
// Profiling
if *profileFlag != "" {
f, err := os.Create(*profileFlag)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// Normal run
reader := bufio.NewReader(os.Stdin)
for {
line, err := reader.ReadString('\n')
if err != nil {
if err != io.EOF {
fmt.Fprintln(os.Stderr, err.Error())
}
break
}
line = strings.TrimSpace(line)
if line == "" || line[0] == '#' {
continue
}
parts := strings.Split(line, " ")
bad := len(parts) != 2
var w, h int
if !bad {
var err1, err2 error
w, err1 = strconv.Atoi(parts[0])
h, err2 = strconv.Atoi(parts[1])
bad = bad || err1 != nil || err2 != nil
}
if bad {
fmt.Fprintf(os.Stderr, "Error: Expected 'width height' got '%s'\n", line)
os.Exit(1)
}
// We use 0 0 as an end of puzzles mark
if w == 0 && h == 0 {
break
}
lines := make([]string, 0, w*h)
for i := 0; i < h; i++ {
line, err := reader.ReadString('\n')
if err != nil {
if err != io.EOF {
fmt.Fprintln(os.Stderr, err.Error())
}
os.Exit(1)
}
line = strings.TrimSpace(line)
lines = append(lines, line)
}
// Done parsing stuff, time for the fun part
p, err := Parse(w, h, lines)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
res := Solve(p)
if !*callsOnlyFlag {
if res {
switch {
case *tubesFlag:
PrintTubes(p, *colorsFlag)
default:
PrintSimple(p, *colorsFlag)
}
} else {
fmt.Println("IMPOSSIBLE")
}
if *callsFlag {
fmt.Printf("Called %d times\n", Calls)
Calls = 0
}
fmt.Println()
}
}
if *callsOnlyFlag {
fmt.Printf("Called %d times\n", Calls)
}
}