-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputs.go
308 lines (276 loc) Β· 6.66 KB
/
inputs.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package aoc
import (
"bufio"
"io"
"math"
"strconv"
"strings"
)
// ReaderToString converts an io.Reader into a string.
func ReaderToString(input io.Reader) string {
scanner := bufio.NewScanner(input)
scanner.Scan()
return scanner.Text()
}
// ReaderToStrings converts an io.Reader into a slice of strings.
func ReaderToStrings(input io.Reader) []string {
scanner := bufio.NewScanner(input)
var res []string
for scanner.Scan() {
res = append(res, scanner.Text())
}
return res
}
// ReaderToInts converts an io.Reader into a slice of strings. It panics in case
// of a parsing error.
func ReaderToInts(input io.Reader) []int {
scanner := bufio.NewScanner(input)
var res []int
for scanner.Scan() {
res = append(res, StringToInt(scanner.Text()))
}
return res
}
// Delimiter implementation.
type Delimiter struct {
ind []int
s string
del string
trimSpace bool
}
type delimiterOptions struct {
trimSpace bool
}
// DelimiterOption holds the Delimiter options.
type DelimiterOption func(options *delimiterOptions)
// WithTrimSpace applies strings.trimSpace on each string.
func WithTrimSpace() DelimiterOption {
return func(options *delimiterOptions) {
options.trimSpace = true
}
}
// NewDelimiter creates a new input delimiter logic.
func NewDelimiter(s, del string, opts ...DelimiterOption) Delimiter {
var options delimiterOptions
for _, opt := range opts {
opt(&options)
}
return Delimiter{
ind: StringFindIndices(s, del),
s: s,
del: del,
trimSpace: options.trimSpace,
}
}
// GetStrings returns all the strings found.
func (d Delimiter) GetStrings() []string {
if len(d.ind) == 0 {
if d.s == "" {
return nil
}
if d.trimSpace {
return []string{strings.TrimSpace(d.s)}
}
return []string{d.s}
}
var res []string
for i := 0; i <= len(d.ind); i++ {
s := d.GetString(i)
if s == "" {
continue
}
res = append(res, s)
}
return res
}
// GetInts returns all the ints found with an optimistic conversion.
func (d Delimiter) GetInts() []int {
return StringsToInts(d.GetStrings())
}
// GetString returns the string at a given index.
func (d Delimiter) GetString(i int) string {
s := ""
if i == 0 {
s = d.s[:d.ind[0]]
} else if i == len(d.ind) {
s = d.s[d.ind[len(d.ind)-1]+len(d.del):]
} else {
s = d.s[d.ind[i-1]+len(d.del) : d.ind[i]]
}
if d.trimSpace {
return strings.TrimSpace(s)
}
return s
}
// GetInt returns the int at a given index with an optimistic conversion.
func (d Delimiter) GetInt(i int) int {
return StringToInt(d.GetString(i))
}
// TryGetInt returns the int at a given index.
func (d Delimiter) TryGetInt(i int) (int, bool) {
return TryStringToInt(d.GetString(i))
}
// IsInt checks whether the value at a given index is an int.
func (d Delimiter) IsInt(i int) bool {
_, err := strconv.Atoi(d.GetString(i))
return err == nil
}
// StringGroups returns groups of lines inputs that are not separated by empty
// lines.
//
// For example:
//
// 0, 1, 2
//
// foo
// bar
//
// Returns {"0, 1, 2"} and {"foo", "bar"}
func StringGroups(lines []string) [][]string {
i := 0
var res [][]string
var row []string
for {
row = append(row, lines[i])
i++
if i >= len(lines) {
res = append(res, row)
break
}
for ; i < len(lines); i++ {
if lines[i] == "" {
break
} else {
row = append(row, lines[i])
}
}
res = append(res, row)
row = nil
i++
if i >= len(lines) {
break
}
}
return res
}
type Board[T any] struct {
Positions map[Position]T
MinRows int
MinCols int
MaxRows int
MaxCols int
}
// ParseBoard parses a board and maps it to a map of Position.
func ParseBoard[T any](lines []string, fn func(r rune, pos Position) T) Board[T] {
positions := make(map[Position]T, len(lines)*len(lines[0]))
for row, line := range lines {
runes := []rune(line)
for col, c := range runes {
positions[NewPosition(row, col)] = fn(c, Position{Row: row, Col: col})
}
}
return Board[T]{
Positions: positions,
MinRows: 0,
MinCols: 0,
MaxRows: len(lines),
MaxCols: len(lines[0]),
}
}
// NewBoard creates a board from a list of positions.
func NewBoard[T any](positions map[Position]T) Board[T] {
board := Board[T]{
Positions: positions,
MinRows: math.MaxInt,
MinCols: math.MaxInt,
MaxRows: math.MinInt,
MaxCols: math.MinInt,
}
for pos := range positions {
board.MinRows = min(board.MinRows, pos.Row)
board.MinCols = min(board.MinCols, pos.Col)
board.MaxRows = max(board.MaxRows, pos.Row+1)
board.MaxCols = max(board.MaxCols, pos.Col+1)
}
return board
}
func NewBoardWithLength[T any](positions map[Position]T, rows, cols int) Board[T] {
board := Board[T]{
Positions: positions,
MinRows: 0,
MinCols: 0,
MaxRows: rows,
MaxCols: cols,
}
return board
}
type BoardElement[T any] struct {
T T
Pos Position
}
func NewBoardFromElements[T any](elements []BoardElement[T], rows, cols int) Board[T] {
positions := make(map[Position]T)
for _, e := range elements {
positions[e.Pos] = e.T
}
board := Board[T]{
Positions: positions,
MinRows: 0,
MinCols: 0,
MaxRows: rows,
MaxCols: cols,
}
return board
}
func NewBoardFromLength[T any](fromRow, toRow, fromCol, toCol int, zero T) Board[T] {
board := Board[T]{
Positions: make(map[Position]T, (toRow-fromRow)*(toCol-fromCol)),
MinRows: fromRow,
MinCols: fromCol,
MaxRows: toRow,
MaxCols: toCol,
}
for row := fromRow; row < toRow; row++ {
for col := fromCol; col < toCol; col++ {
board.Positions[NewPosition(row, col)] = zero
}
}
return board
}
// NewBoardFromReader creates a board from an input reader.
func NewBoardFromReader[T any](input io.Reader, f func(row, col int, r rune) T) Board[T] {
lines := ReaderToStrings(input)
positions := make(map[Position]T, len(lines)*len(lines[0]))
for row, line := range lines {
for col, r := range line {
positions[NewPosition(row, col)] = f(row, col, r)
}
}
return NewBoard(positions)
}
// Get returns the value at a give position.
func (b Board[T]) Get(pos Position) T {
return b.Positions[pos]
}
// Contains checks whether a position is inside a board.
func (b Board[T]) Contains(position Position) bool {
return position.Row >= b.MinRows && position.Row < b.MaxRows &&
position.Col >= b.MinCols && position.Col < b.MaxCols
}
// String returns the board representation.
func (b Board[T]) String(f func(Position, T) rune, missing rune) string {
sb := strings.Builder{}
for row := b.MinRows; row < b.MaxRows; row++ {
for col := b.MinCols; col < b.MaxCols; col++ {
pos := Position{Row: row, Col: col}
if t, exists := b.Positions[pos]; exists {
r := f(pos, t)
sb.WriteRune(r)
} else {
sb.WriteRune(missing)
}
}
sb.WriteString("\n")
}
return sb.String()
}