-
Notifications
You must be signed in to change notification settings - Fork 0
/
d4p1.go
104 lines (84 loc) · 2.24 KB
/
d4p1.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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
)
func main() {
scan := bufio.NewScanner(os.Stdin)
var input []string
var xmas_instances int
validExp := regexp.MustCompile(`XMAS`)
validExp2 := regexp.MustCompile(`SAMX`)
validExps := []*regexp.Regexp{validExp, validExp2}
for scan.Scan() {
input = append(input, scan.Text())
}
for _, exp := range validExps {
for _, line := range input {
expInstances := exp.FindAll([]byte(line), -1)
xmas_instances += len(expInstances)
}
}
// vertical lines
var vertical_input []string
for i := 0; i < len(input); i++ {
var vertical_line string
for j := 0; j < len(input); j++ {
vertical_line += string(input[j][i])
}
vertical_input = append(vertical_input, vertical_line)
}
for _, exp := range validExps {
for _, line := range vertical_input {
expInstances := exp.FindAll([]byte(line), -1)
xmas_instances += len(expInstances)
}
}
// diagonal lines
var diagonal_input []string
for i := 0; i < len(input); i++ {
var diagonal_line string
for j := 0; j <= i; j++ {
diagonal_line += string(input[i-j][j])
}
diagonal_input = append(diagonal_input, diagonal_line)
}
for i := 1; i < len(input); i++ {
var diagonal_line string
for j := i; j < len(input); j++ {
diagonal_line += string(input[len(input)-j+i-1][j])
}
diagonal_input = append(diagonal_input, diagonal_line)
}
for _, exp := range validExps {
for _, line := range diagonal_input {
expInstances := exp.FindAll([]byte(line), -1)
xmas_instances += len(expInstances)
}
}
// opposite diagonal lines
var opposite_diagonal_input []string
for i := 0; i < len(input); i++ {
var diagonal_line string
for j := 0; j <= i; j++ {
diagonal_line += string(input[len(input)-1-i+j][j])
}
opposite_diagonal_input = append(opposite_diagonal_input, diagonal_line)
}
for i := 1; i < len(input); i++ {
var diagonal_line string
for j := i; j < len(input); j++ {
diagonal_line += string(input[0-i+j][j])
}
opposite_diagonal_input = append(opposite_diagonal_input, diagonal_line)
}
for _, exp := range validExps {
for _, line := range opposite_diagonal_input {
expInstances := exp.FindAll([]byte(line), -1)
xmas_instances += len(expInstances)
}
}
fmt.Println(xmas_instances)
}