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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
package aoc23
import (
"bufio"
"fmt"
"io"
"math"
"os"
"strconv"
"strings"
"testing"
)
func assert(t *testing.T, want, got int) {
t.Helper()
if got != want {
t.Errorf("got %d, want %d", got, want)
}
}
func run(f func(r io.Reader) int, r io.Reader) int {
if rc, ok := r.(io.ReadCloser); ok {
defer rc.Close()
}
return f(r)
}
func inputFile(day int) *os.File {
f, err := os.Open(fmt.Sprintf("input/input%02d.txt", day))
if err != nil {
panic(err)
}
return f
}
func inputString(s string) io.Reader { return strings.NewReader(strings.TrimSpace(s)) }
// Parsing
func parseLines[T any](r io.Reader, parser func(line string) T) []T {
scanner := bufio.NewScanner(r)
var values []T
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
values = append(values, parser(line))
}
if err := scanner.Err(); err != nil {
panic(err)
}
return values
}
func atoi(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}
func runes(s string) []rune { return []rune(s) }
func isDigit(r rune) bool { return int(r) >= 48 && int(r) <= 57 }
// Functional
func partial[V1, V2, R any](f func(V1, V2) R, frozenArg V2) func(V1) R {
return func(v1 V1) R { return f(v1, frozenArg) }
}
func compose[V1, R1, R2 any](f1 func(V1) R1, f2 func(R1) R2) func(V1) R2 {
return func(v V1) R2 { return f2(f1(v)) }
}
func reduce[V any](values []V, f func(a, b V) V, initial V) V {
acc := initial
for _, v := range values {
acc = f(acc, v)
}
return acc
}
// map is reserved :(
func transform[V any, R any](values []V, f func(v V) R) []R {
result := make([]R, len(values))
for i, v := range values {
result[i] = f(v)
}
return result
}
func filter[V any](values []V, pred func(v V) bool) []V {
var result []V
for _, v := range values {
if pred(v) {
result = append(result, v)
}
}
return result
}
func quantify[V any](values []V, pred func(v V) bool) int {
n := 0
for _, v := range values {
if pred(v) {
n++
}
}
return n
}
func some[V any](values []V, pred func(v V) bool) bool { return quantify(values, pred) > 0 }
func all[V any](values []V, pred func(v V) bool) bool {
return quantify(values, pred) == len(values)
}
func none[V any](values []V, pred func(v V) bool) bool { return quantify(values, pred) == 0 }
// Math
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func add(a, b int) int { return a + b }
func mul(a, b int) int { return a * b }
func pow(a, b int) int { return int(math.Pow(float64(a), float64(b))) }
func product(ints []int) int { return reduce(ints, mul, 1) }
func sum(ints []int) int { return reduce(ints, add, 0) }
// Collections
type Set[V comparable] struct{ set map[V]bool }
func NewSet[V comparable](vs []V) *Set[V] {
set := Set[V]{}
set.AddAll(vs)
return &set
}
func (s *Set[V]) Add(v V) bool {
_, ok := s.set[v]
if !ok {
if s.set == nil {
s.set = make(map[V]bool)
}
s.set[v] = true
}
return !ok
}
func (s *Set[V]) AddAll(vs []V) bool {
changed := false
for _, v := range vs {
if s.Add(v) {
changed = true
}
}
return changed
}
func (s *Set[V]) Contains(v V) bool {
_, ok := s.set[v]
return ok
}
func (s *Set[V]) Slice() []V {
values := make([]V, 0, len(s.set))
for v := range s.set {
values = append(values, v)
}
return values
}
func (s *Set[V]) Len() int { return len(s.set) }
func (s *Set[V]) Reset() { clear(s.set) }
|