-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
109 lines (97 loc) · 1.71 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
package main
import (
"io"
"github.com/teivah/go-aoc"
)
type cell uint8
const (
empty cell = iota
filled
)
type inputType uint8
const (
lock inputType = iota
key
)
func fs1(input io.Reader) int {
groups := aoc.StringGroups(aoc.ReaderToStrings(input))
var locks [][]int
var keys [][]int
space := 0
for _, group := range groups {
board := aoc.ParseBoard(group, func(r rune, pos aoc.Position) cell {
switch r {
default:
panic(r)
case '.':
return empty
case '#':
return filled
}
})
space = board.MaxRows - 2
t, pins := parse(board)
if t == lock {
locks = append(locks, pins)
} else {
keys = append(keys, pins)
}
}
res := 0
for _, k := range keys {
for _, l := range locks {
fit := true
for i := 0; i < len(k); i++ {
if k[i]+l[i] > space {
fit = false
break
}
}
if fit {
res++
}
}
}
return res
}
func parse(board aoc.Board[cell]) (inputType, []int) {
t := key
if isLock(board) {
t = lock
}
var res []int
if t == lock {
for col := 0; col < board.MaxCols; col++ {
row := 1
for ; row < board.MaxRows; row++ {
if board.Get(aoc.NewPosition(row, col)) != filled {
break
}
}
res = append(res, row-1)
}
} else {
for col := 0; col < board.MaxCols; col++ {
row := board.MaxRows - 2
for ; row >= 0; row-- {
if board.Get(aoc.NewPosition(row, col)) != filled {
break
}
}
res = append(res, board.MaxRows-row-2)
}
}
return t, res
}
func isLock(board aoc.Board[cell]) bool {
for col := 0; col < board.MaxCols; col++ {
if board.Get(aoc.NewPosition(0, col)) != filled {
return false
}
}
return true
}
func fs2(input io.Reader) int {
_ = aoc.ReaderToStrings(input)
return 42
}