Skip to content

Commit 108a914

Browse files
committed
feat: Keep single entrypoint and make it so benchmarking is the default behavior
1 parent f611438 commit 108a914

File tree

4 files changed

+67
-88
lines changed

4 files changed

+67
-88
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ This repository provides a framework and helper tools to streamline the process
77
- `internal/processor.go`: Contains the `PluginProcessor` struct and methods for building plugins, calling functions within those plugins, running tests, and getting input data.
88
- `internal/input.go`: Contains the `Args` struct and functions for processing command-line arguments and determining file paths.
99
- `runner.go`: The main entry point that ties everything together by processing arguments, building the plugin, getting input, running tests, and calling the solution function.
10-
- `benchmark.go`: Alternative entry point that benchmarks every part of every day, and displays the result as a table afterwards.
1110
- It is expected to set up a folder structure following the pattern: `/year/day/partnumber.go`. For example, for the first part of the first day's challenge in 2024, the file should be located at `/2024/day1/part1.go`.
1211
- It is expected to add test files for each day. As a minimal it is adviced to add a test case for each part of a day based on the sample input and output provided by `Advent of Code`.
1312

benchmark.go

Lines changed: 0 additions & 69 deletions
This file was deleted.

internal/input.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Args struct {
1717

1818
func ProcessArgs() Args {
1919
year := flag.String("year", fmt.Sprint(time.Now().Year()), "Year of the challenge")
20-
day := flag.String("day", "1", "Day of the challenge")
20+
day := flag.String("day", "0", "Day of the challenge. 0 to run all days.")
2121
part := flag.String("part", "1", "Part of the challenge")
2222
flag.Parse()
2323

@@ -39,6 +39,10 @@ func ProcessArgs() Args {
3939
}
4040

4141
func (args Args) getPath() (string, error) {
42+
if args.Day == "0" {
43+
return "", nil
44+
}
45+
4246
path := filepath.Join("./", args.Year, "day"+args.Day, "part"+args.Part)
4347
if _, err := os.Stat(filepath.Dir(path)); os.IsNotExist(err) {
4448
return "", fmt.Errorf("the directory %s does not exist", filepath.Dir(path))

runner.go

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,77 @@ package aoc
22

33
import (
44
"fmt"
5+
"os"
6+
"text/tabwriter"
7+
"time"
58

69
internal "github.com/igorwulff/aoc/internal"
710
)
811

9-
func Run() {
10-
args := internal.ProcessArgs()
12+
type BenchmarkResult struct {
13+
TotalTimeInMs int64
14+
}
1115

12-
plugin := internal.PluginProcessor{Args: args}
13-
if err := plugin.Build(); err != nil {
14-
fmt.Println(err)
15-
return
16+
func Run() ([]BenchmarkResult, error) {
17+
args := internal.ProcessArgs()
18+
var plugins []internal.PluginProcessor
19+
// Benchmarking mode
20+
if args.Day == "0" {
21+
allPlugins, err := internal.GetAllPluginProcessors(args)
22+
if err != nil {
23+
fmt.Println(err)
24+
return nil, err
25+
}
26+
plugins = append(plugins, allPlugins...)
27+
} else {
28+
plugins = append(plugins, internal.PluginProcessor{Args: args})
1629
}
1730

18-
input, err := plugin.GetInput()
19-
if err != nil {
20-
fmt.Println(err)
21-
return
22-
}
31+
results := make([]BenchmarkResult, len(plugins))
32+
for i, plugin := range plugins {
33+
input, err := plugin.GetInput()
34+
if err != nil {
35+
fmt.Printf("Could not find input.txt for Day %s Part %s\n", plugin.Args.Day, plugin.Args.Part)
36+
return nil, err
37+
}
2338

24-
plugin.RunTests()
39+
fmt.Printf("Building day %s part %s... ", plugin.Args.Day, plugin.Args.Part)
40+
if err := plugin.Build(); err != nil {
41+
fmt.Print("ERROR!\n")
42+
fmt.Println(err)
43+
return nil, err
44+
}
45+
fmt.Print("OK!\n")
46+
47+
fmt.Print("Executing tests... ")
48+
if err := plugin.RunTests(); err != nil {
49+
fmt.Print("ERROR!\n")
50+
fmt.Println(err)
51+
return nil, err
52+
}
53+
54+
fmt.Print("Executing main function... ")
55+
timeBeforeExec := time.Now().UnixMilli()
56+
output, err := plugin.CallFunc(input)
57+
if err != nil {
58+
fmt.Print("ERROR!\n")
59+
fmt.Println(err)
60+
return nil, err
61+
}
62+
timeAfterExec := time.Now().UnixMilli()
63+
results[i] = BenchmarkResult{TotalTimeInMs: timeAfterExec - timeBeforeExec}
64+
65+
fmt.Printf("Solution: %s\n", output)
66+
}
2567

26-
output, err := plugin.CallFunc(input)
27-
if err != nil {
28-
fmt.Println(err)
29-
return
68+
fmt.Println("Execution finished! Total time:")
69+
w := tabwriter.NewWriter(os.Stdout, 0, 4, 4, ' ', 0)
70+
fmt.Fprintln(w, "Day\tPart\tTime (ms)")
71+
for i, plugin := range plugins {
72+
// @TODO: would be nice to join day & parts in a single row, but for now it's already nice they're alphabetically sorted
73+
fmt.Fprintf(w, "%s\t%s\t%d\n", plugin.Args.Day, plugin.Args.Part, results[i].TotalTimeInMs)
3074
}
75+
w.Flush()
3176

32-
fmt.Println("\nSolution:\n", output)
77+
return results, nil
3378
}

0 commit comments

Comments
 (0)