adventofcode/2018/internal/util/input.go

63 lines
1.2 KiB
Go
Raw Normal View History

2018-12-02 05:40:11 +00:00
package util
import (
"bufio"
2018-12-05 08:49:03 +00:00
"bytes"
"io/ioutil"
2018-12-02 05:40:11 +00:00
"os"
2018-12-11 01:23:18 +00:00
"strconv"
"strings"
2018-12-02 05:40:11 +00:00
)
// ReadInput isn't here to make friends. It is highly specific to this domain.
// It assumes the first argument on the command-line is a file with input. It
// returns an array of strings containing the lines of that file.
func ReadInput() []string {
file, err := os.Open(os.Args[1])
if err != nil {
panic(err)
}
lines := []string{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}
2018-12-05 08:49:03 +00:00
2018-12-11 01:23:18 +00:00
// ReadInputBytes is like ReadInput, but returns a byte array.
2018-12-05 08:49:03 +00:00
func ReadInputBytes() []byte {
rawData, err := ioutil.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
return bytes.TrimRight(rawData, "\n")
}
2018-12-11 01:23:18 +00:00
// ReadInputInts is like ReadInput, but returns an array of ints. (space-separated in the input)
func ReadInputInts() []int {
rawData, err := ioutil.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
strData := strings.Split(strings.TrimSpace(string(rawData)), " ")
data := []int{}
for _, newIntStr := range strData {
newInt, err := strconv.Atoi(newIntStr)
if err != nil {
panic(err)
}
data = append(data, newInt)
}
return data
}