adventofcode/2020/lib/fileutils/fileutils.go
2020-12-11 10:00:29 +00:00

61 lines
1013 B
Go

package fileutils
import (
"bufio"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
)
func InputParserInts(filename string) []int {
file, err := os.Open(filename)
defer file.Close()
if err != nil {
log.Panicf(err.Error())
}
values := make([]int, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
x, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Panicf(err.Error())
}
values = append(values, x)
}
return values
}
func InputParserIntMap(filename string) map[int]bool {
file, err := os.Open(filename)
defer file.Close()
if err != nil {
log.Panicf(err.Error())
}
values := make(map[int]bool)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
x, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Panicf(err.Error())
}
values[x] = true
}
return values
}
func InputParserStrings(filename string) []string {
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Panicf(err.Error())
}
return strings.Split(string(data), "\n")
}