-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (72 loc) · 1.24 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
package main
import (
"fmt"
"slices"
"github.com/devkvlt/aoc"
)
var (
grid = aoc.Lines("input")
height = len(grid)
width = len(grid[0])
)
func valid(x, y int) bool {
return 0 <= x && x < width && 0 <= y && y < height
}
func count1(x, y int) int {
if grid[y][x] != 'X' {
return 0
}
c := 0
for dx := -1; dx <= 1; dx++ {
for dy := -1; dy <= 1; dy++ {
if !valid(x+3*dx, y+3*dy) {
continue
}
word := make([]byte, 4)
for i := range 4 {
word[i] = grid[y+i*dy][x+i*dx]
}
if string(word) == "XMAS" {
c++
}
}
}
return c
}
func part1() {
sum := 0
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
sum += count1(x, y)
}
}
fmt.Println(sum)
}
// M.M M.S S.M S.S
// .A. .A. .A. .A.
// S.S M.S S.M M.M
//
// MMSS MSSM SMMS SSMM
func count2(x, y int) int {
if grid[y][x] != 'A' {
return 0
}
word := string([]byte{grid[y-1][x-1], grid[y-1][x+1], grid[y+1][x+1], grid[y+1][x-1]})
if slices.Contains([]string{"MMSS", "MSSM", "SMMS", "SSMM"}, word) {
return 1
}
return 0
}
func part2() {
sum := 0
for x := 1; x < width-1; x++ {
for y := 1; y < height-1; y++ {
sum += count2(x, y)
}
}
fmt.Println(sum)
}
func main() {
part1() // 2397
part2() // 1824
}