This commit is contained in:
2024-12-04 09:03:27 +00:00
committed by Gareth
parent e2ab6dce9a
commit b1a4969f6d
5 changed files with 253 additions and 3 deletions

View File

@@ -19,9 +19,20 @@ func ToInts(input string, sep string) []int {
}
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]) - strings.Count(lines[0], colSep), len(lines), empty)
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))