From 588561be7c05a63d6cecc9c561d91e60303f38c4 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Mon, 2 Dec 2024 00:23:59 +0000 Subject: [PATCH] embed and read testdata from main --- day-01/internal/one/solve.go | 12 +----------- day-01/internal/two/solve.go | 12 +----------- day-01/main.go | 13 +++++++++++-- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/day-01/internal/one/solve.go b/day-01/internal/one/solve.go index 7ff79ae..24d6f09 100644 --- a/day-01/internal/one/solve.go +++ b/day-01/internal/one/solve.go @@ -3,8 +3,6 @@ package one import ( "bufio" "bytes" - "embed" - "fmt" "io" "math" "regexp" @@ -12,17 +10,9 @@ import ( "github.com/onyx-and-iris/aoc2024/day-01/internal/util" ) -//go:embed testdata -var files embed.FS - var reNums = regexp.MustCompile(`[0-9]+`) -func Solve(filename string) (int, error) { - data, err := files.ReadFile(fmt.Sprintf("testdata/%s", filename)) - if err != nil { - return 0, err - } - +func Solve(data []byte) (int, error) { r := bytes.NewReader(data) left, right, err := parseLines(r) if err != nil { diff --git a/day-01/internal/two/solve.go b/day-01/internal/two/solve.go index 751636f..b0683b2 100644 --- a/day-01/internal/two/solve.go +++ b/day-01/internal/two/solve.go @@ -3,8 +3,6 @@ package two import ( "bufio" "bytes" - "embed" - "fmt" "io" "regexp" "slices" @@ -12,17 +10,9 @@ import ( "github.com/onyx-and-iris/aoc2024/day-01/internal/util" ) -//go:embed testdata -var files embed.FS - var reNums = regexp.MustCompile(`[0-9]+`) -func Solve(filename string) (int, error) { - data, err := files.ReadFile(fmt.Sprintf("testdata/%s", filename)) - if err != nil { - return 0, err - } - +func Solve(data []byte) (int, error) { r := bytes.NewReader(data) left, right, err := parseLines(r) if err != nil { diff --git a/day-01/main.go b/day-01/main.go index 53f26b4..3b5ba78 100644 --- a/day-01/main.go +++ b/day-01/main.go @@ -5,6 +5,7 @@ package main import ( + "embed" "flag" "fmt" "log" @@ -13,17 +14,25 @@ import ( "github.com/onyx-and-iris/aoc2024/day-01/internal/two" ) +//go:embed testdata +var files embed.FS + func main() { filename := flag.String("f", "input.txt", "input file") flag.Parse() - val, err := one.Solve(*filename) + data, err := files.ReadFile(fmt.Sprintf("testdata/%s", *filename)) + if err != nil { + log.Fatal(err) + } + + val, err := one.Solve(data) if err != nil { log.Fatal(err) } fmt.Printf("solution one: %d\n", val) - val, err = two.Solve(*filename) + val, err = two.Solve(data) if err != nil { log.Fatal(err) }