-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
85 lines (77 loc) · 1.69 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
package main
import (
"io"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
board := aoc.NewBoardFromReader(input, func(r rune) rune { return r })
res := 0
word := []rune("XMAS")
for pos := range board.Positions {
res += expand(board, pos, aoc.Up, word)
res += expand(board, pos, aoc.Down, word)
res += expand(board, pos, aoc.Left, word)
res += expand(board, pos, aoc.Right, word)
res += expand(board, pos, aoc.UpLeft, word)
res += expand(board, pos, aoc.UpRight, word)
res += expand(board, pos, aoc.DownLeft, word)
res += expand(board, pos, aoc.DownRight, word)
}
return res
}
func expand(board aoc.Board[rune], pos aoc.Position, dir aoc.Direction, word []rune) int {
if len(word) == 0 {
return 1
}
if !board.Contains(pos) {
return 0
}
if board.Get(pos) != word[0] {
return 0
}
return expand(board, pos.Move(dir, 1), dir, word[1:])
}
func fs2(input io.Reader) int {
board := aoc.NewBoardFromReader(input, func(r rune) rune { return r })
res := 0
for pos := range board.Positions {
res += xmas(board, pos)
}
return res
}
func xmas(board aoc.Board[rune], pos aoc.Position) int {
if board.Get(pos) != 'A' {
return 0
}
upLeft := board.Get(pos.Move(aoc.UpLeft, 1))
upRight := board.Get(pos.Move(aoc.UpRight, 1))
downLeft := board.Get(pos.Move(aoc.DownLeft, 1))
downRight := board.Get(pos.Move(aoc.DownRight, 1))
// First diagonal
switch upLeft {
case 'M':
if downRight != 'S' {
return 0
}
case 'S':
if downRight != 'M' {
return 0
}
default:
return 0
}
// First diagonal
switch upRight {
case 'M':
if downLeft != 'S' {
return 0
}
case 'S':
if downLeft != 'M' {
return 0
}
default:
return 0
}
return 1
}