-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
100 lines (88 loc) · 2.03 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
package main
import (
"io"
"github.com/teivah/go-aoc"
)
func fs1(input io.Reader) int {
board := aoc.ParseBoard(aoc.ReaderToStrings(input), func(r rune, _ aoc.Position) int {
return aoc.RuneToInt(r)
})
count := 0
for pos := range board.Positions {
res := make(map[aoc.Position]struct{})
solve1(board, pos, aoc.UnknownDirection, 0, res)
count += len(res)
}
return count
}
func solve1(board aoc.Board[int], pos aoc.Position, dir aoc.Direction, expected int, res map[aoc.Position]struct{}) {
if !board.Contains(pos) {
return
}
cur := board.Get(pos)
if cur != expected {
return
}
if expected == 9 {
res[pos] = struct{}{}
return
}
rev := dir.Rev()
up := pos.Move(aoc.Up, 1)
down := pos.Move(aoc.Down, 1)
left := pos.Move(aoc.Left, 1)
right := pos.Move(aoc.Right, 1)
if rev != aoc.Up {
solve1(board, up, aoc.Up, expected+1, res)
}
if rev != aoc.Down {
solve1(board, down, aoc.Down, expected+1, res)
}
if rev != aoc.Left {
solve1(board, left, aoc.Left, expected+1, res)
}
if rev != aoc.Right {
solve1(board, right, aoc.Right, expected+1, res)
}
}
func fs2(input io.Reader) int {
board := aoc.ParseBoard(aoc.ReaderToStrings(input), func(r rune, _ aoc.Position) int {
return aoc.RuneToInt(r)
})
count := 0
for pos := range board.Positions {
count += solve2(board, pos, aoc.UnknownDirection, 0)
}
return count
}
func solve2(board aoc.Board[int], pos aoc.Position, dir aoc.Direction, expected int) int {
if !board.Contains(pos) {
return 0
}
cur := board.Get(pos)
if cur != expected {
return 0
}
if expected == 9 {
return 1
}
rev := dir.Rev()
up := pos.Move(aoc.Up, 1)
down := pos.Move(aoc.Down, 1)
left := pos.Move(aoc.Left, 1)
right := pos.Move(aoc.Right, 1)
count := 0
if rev != aoc.Up {
count += solve2(board, up, aoc.Up, expected+1)
}
if rev != aoc.Down {
count += solve2(board, down, aoc.Down, expected+1)
}
if rev != aoc.Left {
count += solve2(board, left, aoc.Left, expected+1)
}
if rev != aoc.Right {
count += solve2(board, right, aoc.Right, expected+1)
}
return count
}