day1
This commit is contained in:
56
2025/go/utils/inputs/inputs.go
Normal file
56
2025/go/utils/inputs/inputs.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package inputs
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"adventofcode2024/utils"
|
||||
"adventofcode2024/utils/grid2d"
|
||||
sparsegrid "adventofcode2024/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
|
||||
}
|
||||
Reference in New Issue
Block a user