summaryrefslogtreecommitdiffstats
path: root/aoc23/util.go
blob: 414756ac552e870840518c49c57ca85064ce8347 (plain) (blame)
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
package aoc23

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
	"testing"
)

type Set[V comparable] struct{ set map[V]bool }

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]) 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) }

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 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 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))
	}
	return values
}

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)) }

func requireInt(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 }

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 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
}

func map2[V any, R any](values []V, f func(v V) R) []R {
	mapped := make([]R, len(values))
	for i, v := range values {
		mapped[i] = f(v)
	}
	return mapped
}

func filter[V any](values []V, pred func(v V) bool) []V {
	var filtered []V
	for _, v := range values {
		if pred(v) {
			filtered = append(filtered, v)
		}
	}
	return filtered
}

func frequency[V any](values []V, pred func(v V) bool) int { return len(filter(values, pred)) }

func anyMatch[V any](values []V, pred func(v V) bool) bool { return frequency(values, pred) > 0 }

func allMatch[V any](values []V, pred func(v V) bool) bool {
	return frequency(values, pred) == len(values)
}

func noneMatch[V any](values []V, pred func(v V) bool) bool { return !anyMatch(values, pred) }

func product(ints []int) int { return reduce(ints, mul, 1) }

func sum(ints []int) int { return reduce(ints, add, 0) }