mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 14:20:48 +00:00
42 lines
800 B
Go
42 lines
800 B
Go
|
package one
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"io"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/onyx-and-iris/aoc2024/day-18/internal/config"
|
||
|
)
|
||
|
|
||
|
func parseLines(r io.Reader, config config.Config) (*graph, error) {
|
||
|
corruptedCoords := [][]int{}
|
||
|
scanner := bufio.NewScanner(r)
|
||
|
for scanner.Scan() {
|
||
|
corruptedCoords = append(corruptedCoords, func() []int {
|
||
|
x := strings.Split(scanner.Text(), ",")
|
||
|
return []int{mustConv(x[0]), mustConv(x[1])}
|
||
|
}())
|
||
|
}
|
||
|
|
||
|
if err := scanner.Err(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return newGraph(config.Width, config.Height, config.NumCorruptions, corruptedCoords), nil
|
||
|
}
|
||
|
|
||
|
func mustConv(s string) int {
|
||
|
n, err := strconv.Atoi(s)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return n
|
||
|
}
|
||
|
|
||
|
func replaceAtIndex(s string, r rune, i int) string {
|
||
|
out := []rune(s)
|
||
|
out[i] = r
|
||
|
return string(out)
|
||
|
}
|