-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (87 loc) · 2.87 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 (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/dowlandaiello/word_search/types"
"github.com/gookit/color"
)
// main is the main word search solver function.
func main() {
var grid *types.Grid // Declare grid buffer
reader := bufio.NewReader(os.Stdin) // Initialize reader
if len(os.Args) == 1 { // Check invalid params
panic("Make sure to pass in word search width and height!") // Panic
} else if len(os.Args) == 2 { // Check import grid
grid = importGrid(os.Args[1]) // Import grid
} else {
gridWidth, err := strconv.Atoi(os.Args[1]) // Parse width
if err != nil { // Check for errors
panic(err) // Panic
}
gridHeight, err := strconv.Atoi(os.Args[2]) // Parse height
if err != nil { // Check for errors
panic(err) // Panic
}
grid = types.NewGrid(uint64(gridWidth), uint64(gridHeight)) // Initialize grid
for i := 0; i < gridHeight; i++ { // Do until made all rows
fmt.Printf("Row %d: ", i) // Log input row
input, err := reader.ReadString('\n') // Read up to \n
if err != nil { // Check for errors
panic(err) // Panic
}
input = strings.Replace(input, "\n", "", -1) // Remove \n
grid.Rows[i].Letters = input // Set letters
}
}
for {
fmt.Print("Word to find: ") // Print prompt
wordToFind, err := reader.ReadString('\n') // Read up to \n
if err != nil { // Check for errors
panic(err) // Panic
}
wordToFind = strings.Replace(wordToFind, "\n", "", -1) // Remove \n
xCoords, yCoords := grid.FindString(wordToFind) // Find word
if len(xCoords) == 0 || len(yCoords) == 0 { // Check couldn't find a match
color.Red.Println("Couldn't find a match!") // Log err
continue // Continue
}
for y, row := range grid.Rows { // Iterate through rows
var rowStr string // Init row buffer
for x, char := range row.Letters { // Iterate through characters
if index := indexOf(xCoords, uint64(x)); index != -1 && yCoords[index] == uint64(y) { // Check matching coordinates
rowStr = rowStr + color.Yellow.Sprintf("%c", char) // Append char
} else {
rowStr = rowStr + string(char) // Append char
}
}
fmt.Println(rowStr) // Log row
}
}
}
// importGrid imports a grid.
func importGrid(fileName string) *types.Grid {
file, err := os.Open(fileName) // Open file
if err != nil { // Check for errors
panic(err) // Panic
}
defer file.Close() // Close file
scanner := bufio.NewScanner(file) // Initialize scanner
var grid types.Grid // Initialize grid buffer
for scanner.Scan() { // Read entire file
line := scanner.Text() // Get line
grid.Rows = append(grid.Rows, types.NewRow(line)) // Set row
}
return &grid // Return read grid
}
// indexOf fetches the index of a value in a uint64 slice.
func indexOf(s []uint64, v uint64) int {
for i, val := range s { // Iterate through values
if val == v { // Check match
return i // Valid
}
}
return -1 // Invalid
}