embed and read testdata from main

This commit is contained in:
onyx-and-iris 2024-12-02 00:23:59 +00:00
parent 40808475ac
commit 588561be7c
3 changed files with 13 additions and 24 deletions

View File

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

View File

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

View File

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