Files
adventofcode/2025/go/utils/inputs/inputs.go
2025-12-04 11:53:17 +00:00

57 lines
1.2 KiB
Go

package inputs
import (
"strings"
"adventofcode2025/utils"
"adventofcode2025/utils/grid2d"
sparsegrid "adventofcode2025/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] {
var width int
lines := strings.Split(input, rowSep)
if colSep == "" {
// If no colSep, width is the length of each line
width = len(lines[0])
} else {
// Use colSep to determine the width of the grid
width = len(strings.Split(lines[0], colSep))
}
grid := grid2d.NewGrid(width, 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
}