aoc2023/day-2/util.go

38 lines
646 B
Go
Raw Normal View History

2023-12-02 20:13:42 +00:00
package main
import (
"bufio"
"log"
"os"
"strconv"
)
// readlines reads lines from stdin.
// Then it returns them as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
// getIdForGame returns the id for a game
func getIdForGame(identifier string) (int, error) {
m := regex["game"].FindStringSubmatch(identifier)
i := regex["game"].SubexpIndex("game")
id, err := strconv.Atoi(m[i])
if err != nil {
return 0, err
}
return id, nil
}