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" "bufio"
"bytes" "bytes"
"embed" "embed"
"fmt"
"io" "io"
"math" "math"
"regexp" "regexp"
@ -16,8 +17,11 @@ var files embed.FS
var reNums = regexp.MustCompile(`[0-9]+`) var reNums = regexp.MustCompile(`[0-9]+`)
func Solve() (int, error) { func Solve(filename string) (int, error) {
data, _ := files.ReadFile("testdata/input.txt") data, err := files.ReadFile(fmt.Sprintf("testdata/%s", filename))
if err != nil {
return 0, err
}
r := bytes.NewReader(data) r := bytes.NewReader(data)
left, right, err := parseLines(r) left, right, err := parseLines(r)

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"embed" "embed"
"fmt"
"io" "io"
"regexp" "regexp"
"slices" "slices"
@ -16,8 +17,11 @@ var files embed.FS
var reNums = regexp.MustCompile(`[0-9]+`) var reNums = regexp.MustCompile(`[0-9]+`)
func Solve() (int, error) { func Solve(filename string) (int, error) {
data, _ := files.ReadFile("testdata/input.txt") data, err := files.ReadFile(fmt.Sprintf("testdata/%s", filename))
if err != nil {
return 0, err
}
r := bytes.NewReader(data) r := bytes.NewReader(data)
left, right, err := parseLines(r) left, right, err := parseLines(r)

View File

@ -5,6 +5,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"log" "log"
@ -13,13 +14,16 @@ import (
) )
func main() { func main() {
val, err := one.Solve() filename := flag.String("f", "input.txt", "input file")
flag.Parse()
val, err := one.Solve(*filename)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Printf("solution one: %d\n", val) fmt.Printf("solution one: %d\n", val)
val, err = two.Solve() val, err = two.Solve(*filename)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }