diff --git a/day-01/internal/one/solve.go b/day-01/internal/one/solve.go index 645f472..7ff79ae 100644 --- a/day-01/internal/one/solve.go +++ b/day-01/internal/one/solve.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "embed" + "fmt" "io" "math" "regexp" @@ -16,8 +17,11 @@ var files embed.FS var reNums = regexp.MustCompile(`[0-9]+`) -func Solve() (int, error) { - data, _ := files.ReadFile("testdata/input.txt") +func Solve(filename string) (int, error) { + data, err := files.ReadFile(fmt.Sprintf("testdata/%s", filename)) + if err != nil { + return 0, err + } r := bytes.NewReader(data) left, right, err := parseLines(r) diff --git a/day-01/internal/two/solve.go b/day-01/internal/two/solve.go index 357b0d6..751636f 100644 --- a/day-01/internal/two/solve.go +++ b/day-01/internal/two/solve.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "embed" + "fmt" "io" "regexp" "slices" @@ -16,8 +17,11 @@ var files embed.FS var reNums = regexp.MustCompile(`[0-9]+`) -func Solve() (int, error) { - data, _ := files.ReadFile("testdata/input.txt") +func Solve(filename string) (int, error) { + data, err := files.ReadFile(fmt.Sprintf("testdata/%s", filename)) + if err != nil { + return 0, err + } r := bytes.NewReader(data) left, right, err := parseLines(r) diff --git a/day-01/main.go b/day-01/main.go index 691e863..53f26b4 100644 --- a/day-01/main.go +++ b/day-01/main.go @@ -5,6 +5,7 @@ package main import ( + "flag" "fmt" "log" @@ -13,13 +14,16 @@ import ( ) func main() { - val, err := one.Solve() + filename := flag.String("f", "input.txt", "input file") + flag.Parse() + + val, err := one.Solve(*filename) if err != nil { log.Fatal(err) } fmt.Printf("solution one: %d\n", val) - val, err = two.Solve() + val, err = two.Solve(*filename) if err != nil { log.Fatal(err) }