You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
854 B
Go

package parser
import (
"fmt"
"io"
"os"
"strconv"
"strings"
)
const DefaultFile = "input"
func ParseFile(input *string) ([]int, error) {
if input == nil {
file := DefaultFile
if len(os.Args) == 2 {
file = os.Args[1]
}
inputFile, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("could not parse input file: %v", err)
}
fileData, err := io.ReadAll(inputFile)
if err != nil {
return nil, fmt.Errorf("could not read input file: %v", err)
}
fileStr := strings.TrimSpace(string(fileData))
input = &fileStr
}
strNums := strings.Split(*input, ",")
opcodes := make([]int, len(strNums))
for i, v := range strNums {
num, err := strconv.Atoi(v)
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: error parsing int from input, substring was %v", v)
}
opcodes[i] = num
}
return opcodes, nil
}