-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.go
69 lines (58 loc) · 1016 Bytes
/
part1.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
package main
import (
"bufio"
"fmt"
"os"
)
func get(x int, y int, grid [][]byte) byte {
if y >= len(grid) || y < 0 || x < 0 {
return 0
}
row := grid[y]
if x >= len(row) {
return 0
}
return row[x]
}
func isAt(x int, y int, grid [][]byte) int {
count := 0
var word []byte = []byte("XMAS")
if get(x, y, grid) != word[0] {
}
for vx := -1; vx <= 1; vx++ {
for vy := -1; vy <= 1; vy++ {
if vx == 0 && vy == 0 {
continue
}
for i := 0; i < len(word); i++ {
nx, ny := x+i*vx, y+i*vy
if get(nx, ny, grid) != word[i] {
break
}
if i == len(word)-1 {
count++
}
}
}
}
return count
}
func main() {
file, _ := os.Open("full.txt")
defer file.Close()
var grid [][]byte
scanner := bufio.NewScanner(file)
sum := 0
for scanner.Scan() {
line := scanner.Text()
row := make([]byte, len(line))
copy(row, line)
grid = append(grid, row)
}
for y, row := range grid {
for x, _ := range row {
sum += isAt(x, y, grid)
}
}
fmt.Println(sum)
}