-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (76 loc) · 1.79 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
package main
import (
"Sudoku/sudoku/large"
"Sudoku/sudoku/medium"
"Sudoku/sudoku/small"
"bytes"
"fmt"
"io"
"log"
"os"
"time"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run main.go <input_file>")
os.Exit(0)
}
now := time.Now()
defer func() {
fmt.Println("Time taken: ", time.Since(now))
}()
file, err := os.Open(os.Args[1])
if err != nil {
log.Fatalf("Error opening the file: %v", err)
}
defer file.Close()
lines, err := lineCounter(file)
if err != nil {
log.Fatalf("Error counting the lines: %v", err)
}
switch lines {
case 16:
board := large.ParseInput(os.Args[1])
candidates := large.Preprocessing(&board)
if large.Backtrack(&board, candidates) {
fmt.Println("The 16x16 Sudoku has been solved. This is the result ->")
large.PrintBoard(board)
} else {
fmt.Println("The 16x16 Sudoku couldn't be solved.")
}
case 9:
board := medium.ParseInput(os.Args[1])
candidates := medium.Preprocessing(&board)
if medium.Backtrack(&board, candidates) {
fmt.Println("The 9x9 Sudoku has been solved. This is the result ->")
medium.PrintBoard(board)
} else {
fmt.Printf("The 9x9 Sudoku couldn't be solved.")
}
case 4:
board := small.ParseInput(os.Args[1])
if small.Backtrack(&board) {
fmt.Println("The 4x4 Sudoku has been solved. This is the result ->")
small.PrintBoard(board)
} else {
fmt.Println("The 4x4 Sudoku couldn't be solved.")
}
default:
fmt.Println("Invalid number of columns. Please enter 4, 9, or 16 in the file.")
}
}
func lineCounter(r io.Reader) (int, error) {
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}
for {
c, err := r.Read(buf)
count += bytes.Count(buf[:c], lineSep)
switch {
case err == io.EOF:
return count + 1, nil
case err != nil:
return count, err
}
}
}