-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
175 lines (158 loc) · 3.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"bufio"
"fmt"
"os"
"slices"
"strings"
)
func main() {
var input []string
for s := bufio.NewScanner(os.Stdin); s.Scan(); {
input = append(input, s.Text())
}
fmt.Printf("Part 1: %d\n", part1(input))
fmt.Printf("Part 2: %d\n", part2(input))
}
func part1(input []string) int {
var sum int
for d := range 8 {
for _, line := range rotate(input, d) {
sum += strings.Count(line, "XMAS")
}
}
return sum
}
func part2(input []string) int {
var sum int
xmas := []string{
"M.M",
".A.",
"S.S",
}
for y := 1; y < len(input)-1; y++ {
for x := 1; x < len(input[0])-1; x++ {
if input[y][x] == 'A' {
var block []string
block = append(block, string(input[y-1][x-1])+"."+string(input[y-1][x+1]))
block = append(block, ".A.")
block = append(block, string(input[y+1][x-1])+"."+string(input[y+1][x+1]))
for _, d := range []int{0, 2, 4, 6} {
if slices.Equal(xmas, rotate(block, d)) {
sum++
break
}
}
}
}
}
return sum
}
// rotate rotates the input counter-clockwise. The lines of the input must have
// equal length. The input is assumed to be ascii.
//
// The direction is a number from 0 to 7:
//
// 0: 0°
// 1: 45°
// 2: 90°
// 3: 135°
// 4: 180°
// 5: 225°
// 6: 270°
// 7: 315°
func rotate(input []string, direction int) []string {
if len(input) == 0 || len(input[0]) == 0 {
return input
}
var out []string
switch direction {
case 0:
out = input
case 1:
for startX := len(input[0]) - 1; startX >= 0; startX-- {
var line []byte
for y, x := 0, startX; y < len(input) && x < len(input[0]); y, x = y+1, x+1 {
line = append(line, input[y][x])
}
out = append(out, string(line))
}
for startY := 1; startY < len(input); startY++ {
var line []byte
for y, x := startY, 0; y < len(input) && x < len(input[0]); y, x = y+1, x+1 {
line = append(line, input[y][x])
}
out = append(out, string(line))
}
case 2:
for x := len(input[0]) - 1; x >= 0; x-- {
var line []byte
for y := 0; y < len(input); y++ {
line = append(line, input[y][x])
}
out = append(out, string(line))
}
case 3:
for startY := len(input) - 1; startY >= 0; startY-- {
var line []byte
for y, x := startY, len(input[0])-1; y < len(input) && x >= 0; y, x = y+1, x-1 {
line = append(line, input[y][x])
}
out = append(out, string(line))
}
for startX := len(input[0]) - 2; startX >= 0; startX-- {
var line []byte
for y, x := 0, startX; y < len(input) && x >= 0; y, x = y+1, x-1 {
line = append(line, input[y][x])
}
out = append(out, string(line))
}
case 4:
for y := len(input) - 1; y >= 0; y-- {
var line []byte
for x := len(input[0]) - 1; x >= 0; x-- {
line = append(line, input[y][x])
}
out = append(out, string(line))
}
case 5:
for startX := 0; startX < len(input[0]); startX++ {
var line []byte
for y, x := len(input)-1, startX; y >= 0 && x >= 0; y, x = y-1, x-1 {
line = append(line, input[y][x])
}
out = append(out, string(line))
}
for startY := len(input) - 2; startY >= 0; startY-- {
var line []byte
for y, x := startY, len(input[0])-1; y >= 0 && x >= 0; y, x = y-1, x-1 {
line = append(line, input[y][x])
}
out = append(out, string(line))
}
case 6:
for x := 0; x < len(input[0]); x++ {
var line []byte
for y := len(input) - 1; y >= 0; y-- {
line = append(line, input[y][x])
}
out = append(out, string(line))
}
case 7:
for startY := 0; startY < len(input); startY++ {
var line []byte
for y, x := startY, 0; y >= 0 && x < len(input); y, x = y-1, x+1 {
line = append(line, input[y][x])
}
out = append(out, string(line))
}
for startX := 1; startX < len(input[0]); startX++ {
var line []byte
for y, x := len(input)-1, startX; y >= 0 && x < len(input[0]); y, x = y-1, x+1 {
line = append(line, input[y][x])
}
out = append(out, string(line))
}
}
return out
}