summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Vink <mike1994vink@gmail.com>2023-02-26 18:58:04 +0100
committerMike Vink <mike1994vink@gmail.com>2023-02-26 18:58:04 +0100
commitdd496570e3ebaedb37d713d8113fecac2b92b482 (patch)
tree03d5c1a646d75ddbf9aac80277ea828f6d0c58dd
parent92e0fe693cdca3b15fb3c0e9b97738fdac0df8ba (diff)
fixup
-rw-r--r--2022/01/calories/elfs.go6
-rw-r--r--2022/01/main.go2
-rw-r--r--aoc/day.go4
-rw-r--r--cmd/aocli/gen.go23
4 files changed, 25 insertions, 10 deletions
diff --git a/2022/01/calories/elfs.go b/2022/01/calories/elfs.go
index 2011fc8..a7f747c 100644
--- a/2022/01/calories/elfs.go
+++ b/2022/01/calories/elfs.go
@@ -5,11 +5,11 @@ import (
"strconv"
)
-type solver func(ctx context.Context, data []int) ([]int, error)
-
var (
Reader = readCaloriesInts
- Solvers = []solver{partOne, partTwo}
+ Solvers = []func(ctx context.Context, data []int) ([]int, error){
+ partOne, partTwo,
+ }
)
func partOne(ctx context.Context, data []int) ([]int, error) {
diff --git a/2022/01/main.go b/2022/01/main.go
index 51d5750..9b564e9 100644
--- a/2022/01/main.go
+++ b/2022/01/main.go
@@ -14,6 +14,6 @@ func main() {
context.TODO(),
aoc.NewScanCloser("2022/01/input.txt"),
aoc.ReadByLine(calories.Reader),
- calories.Solvers...,
+ calories.Solvers,
)
}
diff --git a/aoc/day.go b/aoc/day.go
index 1258e46..b47607b 100644
--- a/aoc/day.go
+++ b/aoc/day.go
@@ -25,9 +25,9 @@ func NewScanCloser(file string) *ScanCloser {
}
}
-type Solver[T any, R any] func(ctx context.Context, data T) (R, error)
+type Solvers[T any, R any] []func(ctx context.Context, data T) (R, error)
-func RunDay[T any, R any](ctx context.Context, s *ScanCloser, in Reader[T], solvers ...Solver[T, R]) error {
+func RunDay[T any, R any](ctx context.Context, s *ScanCloser, in Reader[T], solvers Solvers[T, R]) error {
defer s.Closer.Close()
data, err := in(ctx, s.Scanner)
diff --git a/cmd/aocli/gen.go b/cmd/aocli/gen.go
index 7320894..463e3b7 100644
--- a/cmd/aocli/gen.go
+++ b/cmd/aocli/gen.go
@@ -2,6 +2,8 @@ package main
import (
"fmt"
+ "io/fs"
+ "os"
"strconv"
"strings"
@@ -15,16 +17,17 @@ var genCmd = &cobra.Command{
Use: "gen",
Short: "Generate a new boilerplate entrypoint for a day of Advent of Code",
RunE: func(cmd *cobra.Command, args []string) error {
- year, day := "", ""
- if len(args) == 1 {
+ year, day, packageName := "", "", ""
+ if len(args) == 2 {
parts := strings.SplitN(args[0], "/", 2)
if len(parts) == 2 {
year = parts[0]
day = parts[1]
}
} else {
- return fmt.Errorf("Expected one argument in format %q", "<year>/<day>")
+ return fmt.Errorf("Expected two argument in format %q %q", "<year>/<day>", "<packageName>")
}
+ packageName = fmt.Sprintf("%s", args[1])
if year == "" || day == "" {
return fmt.Errorf("Expected one argument in format %q", "<year>/<day>")
@@ -45,7 +48,19 @@ var genCmd = &cobra.Command{
}
dayStr := fmt.Sprintf("%02d", dayInt)
- fmt.Println(dayStr)
+
+ fmt.Println(dayStr, packageName)
+ if err := ensureDir(fmt.Sprintf("%s/%s", year, dayStr)); err != nil {
+ return fmt.Errorf("Couldn't create directory %s/%s: %v", year, dayStr, err)
+ }
return nil
},
}
+
+func ensureDir(relativePath string) error {
+ _, err := os.Stat(relativePath)
+ if os.IsNotExist(err) {
+ return os.MkdirAll(relativePath, fs.ModePerm)
+ }
+ return nil
+}