Files
advent-of-code-2023-go/utils/utils.go
2023-12-02 20:16:27 +01:00

39 lines
613 B
Go

package utils
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func PanicOnErr(err error) {
if err != nil {
panic(err)
}
}
func MustAtoi(s string) int {
v, err := strconv.Atoi(s)
PanicOnErr(err)
return v
}
func Readfile(day int, example bool) string {
filename := fmt.Sprintf("input/day%02d.txt", day)
if example {
filename = fmt.Sprintf("input/day%02d_example.txt", day)
}
file, err := os.Open(filename)
PanicOnErr(err)
defer file.Close()
reader := bufio.NewReader(file)
contents, err := io.ReadAll(reader)
PanicOnErr(err)
return strings.TrimSuffix(string(contents), "\n")
}