-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.go
67 lines (58 loc) · 1.38 KB
/
part2.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
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 {
matrices := [][4]int{
{1, 0, 0, 1}, // Identity / No Change
{0, -1, 1, 0}, // 90° Counterclockwise
{-1, 0, 0, -1}, // 180°
{0, 1, -1, 0}, // 270° Counterclockwise (or 90° Clockwise)
{1, 0, 0, -1}, // Reflection across x-axis
{-1, 0, 0, 1}, // Reflection across y-axis
{0, 1, 1, 0}, // Reflection across y = x
{0, -1, -1, 0}, // Reflection across y = -x
}
for _, ele := range matrices {
if string(get(x, y, grid)) == "A" &&
string(get(x-ele[0]-ele[1], y-ele[2]-ele[3], grid)) == "M" &&
string(get(x-ele[0]+ele[1], y-ele[2]+ele[3], grid)) == "M" &&
string(get(x+ele[0]+ele[1], y+ele[2]+ele[3], grid)) == "S" &&
string(get(x+ele[0]-ele[1], y+ele[2]-ele[3], grid)) == "S" {
fmt.Println("found at ", x, y)
return 1
}
}
return 0
}
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)
}