-
Notifications
You must be signed in to change notification settings - Fork 0
/
grids.go
113 lines (103 loc) · 2.47 KB
/
grids.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
package libaoc
import "fmt"
// These are based on AoC2024 D4
// Coord holds {Row, Column} (I always get confuzzled with x,y)
type Coord struct {
r int // row
c int // col
}
// Grid is the map containing the strings
type Grid map[Coord]string
// StringsMatrix is the object containing a Grid and documents the height and width
type StringsMatrix struct {
grid Grid
height int
width int
}
// buildMatrix is a method to build the matrix.
func (m *StringsMatrix) buildMatrix(input []string) {
m.grid = make(Grid)
(*m).height = len(input) - 1
for r, line := range input {
if (*m).width == 0 {
(*m).width = len(line) - 1
}
for c, char := range line {
m.grid[Coord{r, c}] = string(char)
}
}
}
// printMatrix is a method to visually validate the matrix.
func (m *StringsMatrix) printMatrix() {
fmt.Println()
var line string
for r := 0; r < (*m).height; r++ {
for c := 0; c < (*m).width; c++ {
if c == 0 {
line = ""
}
line = line + (*m).grid[Coord{r, c}]
}
fmt.Printf("%s\n", line)
}
fmt.Println()
}
// inMatrix is a method to validate if a point is in the matrix.
func (m *StringsMatrix) inmatrix(coord Coord) bool {
_, ok := m.grid[coord]
return ok
}
// wordSearch is a method to find the number of occurrences for a word in all directions, horizontal, vertical, diagonal
func (m *StringsMatrix) wordSearch(word string) (occurrences int) {
firstLetter := string(word[0])
directions := []string{"n", "ne", "e", "se", "s", "sw", "w", "nw"}
for r := 0; r < (*m).height; r++ {
for c := 0; c < (*m).width; c++ {
switch m.grid[Coord{r, c}] {
case firstLetter:
// fmt.Printf("Found X: i %d, j %d\n", i, j)
for _, direction := range directions {
if m.searchDirection(direction, word, r, c) {
occurrences++
}
}
}
}
}
return occurrences
}
// searchDirection is a method that searches a "word" in a "direction", starting at i,j
func (m *StringsMatrix) searchDirection(direction, word string, r, c int) (found bool) {
for _, letter := range word {
if !(*m).inmatrix(Coord{r, c}) {
return false
}
if (*m).grid[Coord{r, c}] == string(letter) { // If the letter is correct, prep for the next
switch direction {
case "n":
r--
case "e":
c++
case "s":
r++
case "w":
c--
case "ne":
c++
r--
case "nw":
c--
r--
case "se":
c++
r++
case "sw":
c--
r++
}
} else { // The found letter was different
return false
}
}
return true // We never had a miss
}