This commit is contained in:
Gareth Evans
2023-12-01 10:12:50 +00:00
parent 8d5196a10f
commit 2843713a0b
13 changed files with 1556 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
package inputs
import (
"strings"
"adventofcode2022/utils"
"adventofcode2022/utils/grid2d"
sparsegrid "adventofcode2022/utils/sparseGrid"
)
func ToInts(input string, sep string) []int {
var r []int
for _, line := range strings.Split(input, sep) {
if line != "" {
r = append(r, utils.MustAtoi(line))
}
}
return r
}
func ToGrid2D[T any](input, rowSep, colSep string, empty T, conv func(string) T) *grid2d.Grid[T] {
lines := strings.Split(input, rowSep)
grid := grid2d.NewGrid(len(lines[0]), len(lines), empty)
for y, line := range lines {
for x, v := range strings.Split(line, colSep) {
grid.Set(x, y, conv(v))
}
}
return grid
}
func ToSparseGrid[T comparable](input, rowSep, colSep string, empty T, conv func(string) T) *sparsegrid.SparseGrid[T] {
lines := strings.Split(input, rowSep)
grid := sparsegrid.NewGrid(empty)
for y, line := range lines {
for x, v := range strings.Split(line, colSep) {
grid.Set(x, y, conv(v))
}
}
return grid
}