From 3c487337f5a923b52d7fd9bd111005e2ea983305 Mon Sep 17 00:00:00 2001 From: Dylan Butler Date: Mon, 9 Dec 2024 22:10:56 +1100 Subject: [PATCH] runtime input --- 2024/01/01.go | 6 +--- 2024/01/02.go | 7 +--- 2024/02/01.go | 7 +--- 2024/02/02.go | 6 +--- 2024/03/01.go | 6 +--- 2024/03/02.go | 6 +--- 2024/04/01.go | 6 +--- 2024/04/02.go | 7 +--- 2024/05/01.go | 6 +--- 2024/05/02.go | 6 +--- 2024/06/01.go | 7 +--- 2024/06/02.go | 7 +--- 2024/07/01.go | 7 +--- 2024/07/02.go | 7 +--- 2024/08/01.go | 11 ++---- 2024/08/02.go | 7 ++-- 2024/09/01.go | 7 +--- 2024/09/02.go | 6 +--- pkg/harness/harness.go | 79 ++++++++++++++++++++++++++++++++++++++---- 19 files changed, 92 insertions(+), 109 deletions(-) diff --git a/2024/01/01.go b/2024/01/01.go index c7c7754..d22b307 100644 --- a/2024/01/01.go +++ b/2024/01/01.go @@ -1,7 +1,6 @@ package main import ( - "embed" "slices" "github.com/dbut2/advent-of-code/pkg/harness" @@ -26,9 +25,6 @@ func solve(input [][]int) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/01/02.go b/2024/01/02.go index 81a813e..c0a3a8f 100644 --- a/2024/01/02.go +++ b/2024/01/02.go @@ -1,8 +1,6 @@ package main import ( - "embed" - "github.com/dbut2/advent-of-code/pkg/harness" ) @@ -26,10 +24,7 @@ func solve(input [][]int) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 31) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/02/01.go b/2024/02/01.go index cfffce5..66152e7 100644 --- a/2024/02/01.go +++ b/2024/02/01.go @@ -1,8 +1,6 @@ package main import ( - "embed" - "github.com/dbut2/advent-of-code/pkg/harness" "github.com/dbut2/advent-of-code/pkg/math" ) @@ -35,10 +33,7 @@ func solve(input [][]int) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 2) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/02/02.go b/2024/02/02.go index c5392ac..187ecaa 100644 --- a/2024/02/02.go +++ b/2024/02/02.go @@ -1,7 +1,6 @@ package main import ( - "embed" "slices" "github.com/dbut2/advent-of-code/pkg/harness" @@ -39,10 +38,7 @@ func isSafe(ints []int) bool { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 4) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/03/01.go b/2024/03/01.go index b0d8841..274be7a 100644 --- a/2024/03/01.go +++ b/2024/03/01.go @@ -1,7 +1,6 @@ package main import ( - "embed" "regexp" "github.com/dbut2/advent-of-code/pkg/harness" @@ -21,10 +20,7 @@ func solve(input string) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 161) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/03/02.go b/2024/03/02.go index 56e77e8..17915f8 100644 --- a/2024/03/02.go +++ b/2024/03/02.go @@ -1,7 +1,6 @@ package main import ( - "embed" "regexp" "github.com/dbut2/advent-of-code/pkg/harness" @@ -31,10 +30,7 @@ func solve(input string) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(2, 48) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/04/01.go b/2024/04/01.go index 716cdd0..5d1691d 100644 --- a/2024/04/01.go +++ b/2024/04/01.go @@ -1,7 +1,6 @@ package main import ( - "embed" "slices" "github.com/dbut2/advent-of-code/pkg/harness" @@ -32,9 +31,6 @@ func solve(input space.Grid[byte]) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/04/02.go b/2024/04/02.go index fda7887..e506f17 100644 --- a/2024/04/02.go +++ b/2024/04/02.go @@ -1,8 +1,6 @@ package main import ( - "embed" - "github.com/dbut2/advent-of-code/pkg/harness" "github.com/dbut2/advent-of-code/pkg/space" ) @@ -38,9 +36,6 @@ func rotate(dirs []space.Direction) []space.Direction { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/05/01.go b/2024/05/01.go index 50bf91b..b754786 100644 --- a/2024/05/01.go +++ b/2024/05/01.go @@ -1,7 +1,6 @@ package main import ( - "embed" "slices" "strings" @@ -49,10 +48,7 @@ func solve(input [2][]string) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 143) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/05/02.go b/2024/05/02.go index b6ba16f..98b645b 100644 --- a/2024/05/02.go +++ b/2024/05/02.go @@ -1,7 +1,6 @@ package main import ( - "embed" "slices" "strings" @@ -95,10 +94,7 @@ func solve(input [2][]string) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 123) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/06/01.go b/2024/06/01.go index 31bc8f3..3b316b1 100644 --- a/2024/06/01.go +++ b/2024/06/01.go @@ -1,8 +1,6 @@ package main import ( - "embed" - "github.com/dbut2/advent-of-code/pkg/harness" "github.com/dbut2/advent-of-code/pkg/sets" "github.com/dbut2/advent-of-code/pkg/space" @@ -32,10 +30,7 @@ func solve(input space.Grid[byte]) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 41) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/06/02.go b/2024/06/02.go index 52be5dd..bae98f9 100644 --- a/2024/06/02.go +++ b/2024/06/02.go @@ -1,8 +1,6 @@ package main import ( - "embed" - "github.com/dbut2/advent-of-code/pkg/harness" "github.com/dbut2/advent-of-code/pkg/sets" "github.com/dbut2/advent-of-code/pkg/space" @@ -53,10 +51,7 @@ func solve(input space.Grid[byte]) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 6) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/07/01.go b/2024/07/01.go index 9b6f68b..826bec9 100644 --- a/2024/07/01.go +++ b/2024/07/01.go @@ -1,8 +1,6 @@ package main import ( - "embed" - "github.com/dbut2/advent-of-code/pkg/harness" ) @@ -25,10 +23,7 @@ func calc(goal int, running int, nums []int) bool { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 3749) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/07/02.go b/2024/07/02.go index 1f4e7ff..646ef64 100644 --- a/2024/07/02.go +++ b/2024/07/02.go @@ -1,8 +1,6 @@ package main import ( - "embed" - "github.com/dbut2/advent-of-code/pkg/harness" "github.com/dbut2/advent-of-code/pkg/math" ) @@ -39,10 +37,7 @@ func log10(a int) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 11387) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/08/01.go b/2024/08/01.go index a99d634..fd92eb1 100644 --- a/2024/08/01.go +++ b/2024/08/01.go @@ -1,7 +1,7 @@ package main import ( - "embed" + "time" "github.com/dbut2/advent-of-code/pkg/harness" "github.com/dbut2/advent-of-code/pkg/sets" @@ -36,15 +36,8 @@ func solve(input space.Grid[byte]) int { return len(antinodes) } -func diff(a, b space.Cell) space.Direction { - return space.Direction{a[0] - b[0], a[1] - b[1]} -} - func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 14) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/08/02.go b/2024/08/02.go index 20c69f3..30ad776 100644 --- a/2024/08/02.go +++ b/2024/08/02.go @@ -1,7 +1,7 @@ package main import ( - "embed" + "time" "github.com/dbut2/advent-of-code/pkg/harness" "github.com/dbut2/advent-of-code/pkg/sets" @@ -43,11 +43,8 @@ func diff(a, b space.Cell) space.Direction { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 34) h.Expect(2, 9) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/09/01.go b/2024/09/01.go index c5ffb24..d8962c9 100644 --- a/2024/09/01.go +++ b/2024/09/01.go @@ -1,8 +1,6 @@ package main import ( - "embed" - "github.com/dbut2/advent-of-code/pkg/harness" ) @@ -44,10 +42,7 @@ func solve(input string) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 1928) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/2024/09/02.go b/2024/09/02.go index 0312499..0075227 100644 --- a/2024/09/02.go +++ b/2024/09/02.go @@ -1,7 +1,6 @@ package main import ( - "embed" "slices" "github.com/dbut2/advent-of-code/pkg/harness" @@ -52,10 +51,7 @@ func solve(input string) int { } func main() { - h := harness.New(solve, inputs) + h := harness.New(solve) h.Expect(1, 2858) h.Run() } - -//go:embed *.txt -var inputs embed.FS diff --git a/pkg/harness/harness.go b/pkg/harness/harness.go index 7d92924..855fcf5 100644 --- a/pkg/harness/harness.go +++ b/pkg/harness/harness.go @@ -1,13 +1,19 @@ package harness import ( - "embed" "fmt" + "io" + "net/http" + "os" + "path/filepath" + "regexp" + "runtime" strings2 "strings" "github.com/dbut2/advent-of-code/pkg/benchmark" "github.com/dbut2/advent-of-code/pkg/lists" "github.com/dbut2/advent-of-code/pkg/space" + "github.com/dbut2/advent-of-code/pkg/sti" "github.com/dbut2/advent-of-code/pkg/strings" ) @@ -16,8 +22,8 @@ import ( type Harness[T any, U comparable] struct { preProcessor PreProcessor[T] solve func(T) U - inputs embed.FS silent bool + metadata metadata } // HarnessOpt modifies the Harness when initialising. @@ -38,11 +44,11 @@ func WithSilence[T any, U comparable]() HarnessOpt[T, U] { // New returns a new Harness. At minimum a solve function and some inputs are // required. -func New[T any, U comparable](solve func(T) U, inputs embed.FS, opts ...HarnessOpt[T, U]) *Harness[T, U] { +func New[T any, U comparable](solve func(T) U, opts ...HarnessOpt[T, U]) *Harness[T, U] { h := Harness[T, U]{ preProcessor: defaultPreProcessor[T](), solve: solve, - inputs: inputs, + metadata: getMetadata(), } for _, opt := range opts { @@ -52,6 +58,26 @@ func New[T any, U comparable](solve func(T) U, inputs embed.FS, opts ...HarnessO return &h } +type metadata struct { + workdir string + year, day int +} + +func getMetadata() metadata { + _, file, _, _ := runtime.Caller(2) + + dir := filepath.Dir(file) + + r := regexp.MustCompile(`(\d{4})/(\d{2})/.+\.go`) + parts := r.FindStringSubmatch(file) + + return metadata{ + workdir: dir, + year: sti.Int(parts[1]), + day: sti.Int(parts[2]), + } +} + // PreProcessor is a function that process the input data before passing to the // solve function. type PreProcessor[T any] func(string) T @@ -129,15 +155,54 @@ func (h *Harness[T, U]) Run() { } func (h *Harness[T, U]) readInput(s string) string { - input, err := h.inputs.ReadFile(fmt.Sprintf("%s.txt", s)) + data, _ := os.ReadFile(filepath.Join(h.metadata.workdir, fmt.Sprintf("%s.txt", s))) + return string(data) +} + +func (h *Harness[T, U]) writeInput(s string, input string) { + err := os.WriteFile(filepath.Join(h.metadata.workdir, fmt.Sprintf("%s.txt", s)), []byte(input), 0644) if err != nil { panic(err.Error()) } - return string(input) } func (h *Harness[T, U]) getInput() string { - return h.readInput("input") + input := h.readInput("input") + if input == "" { + input = h.fetchInput() + h.writeInput("input", input) + } + return input +} + +func (h *Harness[T, U]) fetchInput() string { + url := fmt.Sprintf("https://adventofcode.com/%d/day/%d/input", h.metadata.year, h.metadata.day) + session := os.Getenv("AOC_SESSION") + if session == "" { + panic("no AOC_SESSION env") + } + + req, err := http.NewRequest("GET", url, nil) + if err != nil { + panic(err.Error()) + } + req.AddCookie(&http.Cookie{ + Name: "session", + Value: session, + }) + + resp, err := http.DefaultClient.Do(req) + if err != nil { + panic(err.Error()) + } + defer resp.Body.Close() + + bytes, err := io.ReadAll(resp.Body) + if err != nil { + panic(err.Error()) + } + + return string(bytes) } func (h *Harness[T, U]) getTestInput(n int) string {