add -f flag for passing filename

This commit is contained in:
onyx-and-iris 2024-12-01 23:12:34 +00:00
parent 947016d43b
commit 40808475ac
3 changed files with 18 additions and 6 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)
}