diff --git a/.scaffold/aoc/hooks/post_scaffold.sh b/.scaffold/aoc/hooks/post_scaffold.sh new file mode 100644 index 0000000..7be0bdd --- /dev/null +++ b/.scaffold/aoc/hooks/post_scaffold.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +cd "{{ .Project }}" || exit 1 + +go mod init github.com/onyx-and-iris/aoc{{ .Scaffold.year }}/"{{ .Project }}" +go mod tidy + +# shellcheck disable=SC2001 +day=$(echo "{{ .Project }}" | sed 's/^day-0*//') + +mkdir -p testdata +curl "https://adventofcode.com/{{ .Scaffold.year }}/day/$day/input" -H "Cookie: session=$AOC_SESSION" > testdata/input.txt + + +mkdir -p cmd/cli/testdata && ln testdata/input.txt cmd/cli/testdata/input.txt +mkdir -p internal/one/testdata && ln testdata/input.txt internal/one/testdata/input.txt +mkdir -p internal/two/testdata && ln testdata/input.txt internal/two/testdata/input.txt diff --git a/.scaffold/aoc/scaffold.yml b/.scaffold/aoc/scaffold.yml new file mode 100644 index 0000000..ed9ce78 --- /dev/null +++ b/.scaffold/aoc/scaffold.yml @@ -0,0 +1,16 @@ +messages: + pre: | + Use this scaffold to generate a new AOC project. + post: | + # You've generated a new AOC project! + + You can now run the following commands: + + - `go run ./cmd/cli/` - Runs the main application and supports passing CLI args + +questions: + - name: year + prompt: + message: "What year is this project for?" + default: "2024" + type: "input" \ No newline at end of file diff --git a/.scaffold/aoc/{{ .Project }}/cmd/cli/main.go b/.scaffold/aoc/{{ .Project }}/cmd/cli/main.go new file mode 100644 index 0000000..b6aff77 --- /dev/null +++ b/.scaffold/aoc/{{ .Project }}/cmd/cli/main.go @@ -0,0 +1,41 @@ +/******************************************************************************** + Advent of Code {{ .Scaffold.year }} - {{ .Project }} +********************************************************************************/ + +package main + +import ( + "embed" + "flag" + "fmt" + "slices" + + log "github.com/sirupsen/logrus" + + problems "github.com/onyx-and-iris/aoc{{ .Scaffold.year }}/{{ .Project }}" +) + +//go:embed testdata +var files embed.FS + +func main() { + filename := flag.String("f", "input.txt", "input file") + loglevel := flag.Int("l", int(log.InfoLevel), "log level") + flag.Parse() + + if slices.Contains(log.AllLevels, log.Level(*loglevel)) { + log.SetLevel(log.Level(*loglevel)) + } + + data, err := files.ReadFile(fmt.Sprintf("testdata/%s", *filename)) + if err != nil { + log.Fatal(err) + } + + one, two, err := problems.Solve(data) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("solution one: %d\nsolution two: %d\n", one, two) +} diff --git a/.scaffold/aoc/{{ .Project }}/internal/one/solve.go b/.scaffold/aoc/{{ .Project }}/internal/one/solve.go new file mode 100644 index 0000000..9950929 --- /dev/null +++ b/.scaffold/aoc/{{ .Project }}/internal/one/solve.go @@ -0,0 +1,29 @@ +package one + +import ( + "bufio" + "bytes" + "io" +) + +func Solve(buf []byte) (int, error) { + r := bytes.NewReader(buf) + err := parseLines(r) + if err != nil { + return 0, err + } + + return 0, nil +} + +func parseLines(r io.Reader) error { + scanner := bufio.NewScanner(r) + for scanner.Scan() { + } + + if err := scanner.Err(); err != nil { + return err + } + + return nil +} diff --git a/.scaffold/aoc/{{ .Project }}/internal/one/solve_internal_test.go b/.scaffold/aoc/{{ .Project }}/internal/one/solve_internal_test.go new file mode 100644 index 0000000..e5fd7ea --- /dev/null +++ b/.scaffold/aoc/{{ .Project }}/internal/one/solve_internal_test.go @@ -0,0 +1,15 @@ +package one + +import ( + _ "embed" + "os" + "testing" +) + +//go:embed testdata/input.txt +var data []byte + +func BenchmarkSolve(b *testing.B) { + os.Stdout, _ = os.Open(os.DevNull) + Solve(data) +} diff --git a/.scaffold/aoc/{{ .Project }}/internal/one/util.go b/.scaffold/aoc/{{ .Project }}/internal/one/util.go new file mode 100644 index 0000000..f1fca11 --- /dev/null +++ b/.scaffold/aoc/{{ .Project }}/internal/one/util.go @@ -0,0 +1,18 @@ +package one + +import ( + "bufio" + "io" +) + +func parseLines(r io.Reader) error { + scanner := bufio.NewScanner(r) + for scanner.Scan() { + } + + if err := scanner.Err(); err != nil { + return err + } + + return nil +} diff --git a/.scaffold/aoc/{{ .Project }}/internal/two/solve.go b/.scaffold/aoc/{{ .Project }}/internal/two/solve.go new file mode 100644 index 0000000..b6d5012 --- /dev/null +++ b/.scaffold/aoc/{{ .Project }}/internal/two/solve.go @@ -0,0 +1,29 @@ +package two + +import ( + "bufio" + "bytes" + "io" +) + +func Solve(buf []byte) (int, error) { + r := bytes.NewReader(buf) + err := parseLines(r) + if err != nil { + return 0, err + } + + return 0, nil +} + +func parseLines(r io.Reader) error { + scanner := bufio.NewScanner(r) + for scanner.Scan() { + } + + if err := scanner.Err(); err != nil { + return err + } + + return nil +} diff --git a/.scaffold/aoc/{{ .Project }}/internal/two/solve_internal_test.go b/.scaffold/aoc/{{ .Project }}/internal/two/solve_internal_test.go new file mode 100644 index 0000000..ecb05d5 --- /dev/null +++ b/.scaffold/aoc/{{ .Project }}/internal/two/solve_internal_test.go @@ -0,0 +1,15 @@ +package two + +import ( + _ "embed" + "os" + "testing" +) + +//go:embed testdata/input.txt +var data []byte + +func BenchmarkSolve(b *testing.B) { + os.Stdout, _ = os.Open(os.DevNull) + Solve(data) +} diff --git a/.scaffold/aoc/{{ .Project }}/internal/two/util.go b/.scaffold/aoc/{{ .Project }}/internal/two/util.go new file mode 100644 index 0000000..eb884f3 --- /dev/null +++ b/.scaffold/aoc/{{ .Project }}/internal/two/util.go @@ -0,0 +1,18 @@ +package two + +import ( + "bufio" + "io" +) + +func parseLines(r io.Reader) error { + scanner := bufio.NewScanner(r) + for scanner.Scan() { + } + + if err := scanner.Err(); err != nil { + return err + } + + return nil +} diff --git a/.scaffold/aoc/{{ .Project }}/makefile b/.scaffold/aoc/{{ .Project }}/makefile new file mode 100644 index 0000000..49ffacb --- /dev/null +++ b/.scaffold/aoc/{{ .Project }}/makefile @@ -0,0 +1,33 @@ +program = {{ .Project }} + +GO = go +SRC_DIR := src +BIN_DIR := bin + +EXE := $(BIN_DIR)/$(program) + +.DEFAULT_GOAL := build + +.PHONY: fmt vet build test bench clean +fmt: + $(GO) fmt ./... + +vet: fmt + $(GO) vet ./... + +build: vet | $(BIN_DIR) + $(GO) build -o $(EXE) ./$(SRC_DIR) + +test: + $(GO) test ./... + +bench: + $(GO) test ./internal/one/ -bench=. > internal/one/benchmark + $(GO) test ./internal/two/ -bench=. > internal/two/benchmark + $(GO) test . -count=10 -bench=. > benchmark + +$(BIN_DIR): + @mkdir -p $@ + +clean: + @rm -rv $(BIN_DIR) \ No newline at end of file diff --git a/.scaffold/aoc/{{ .Project }}/solve.go b/.scaffold/aoc/{{ .Project }}/solve.go new file mode 100644 index 0000000..def85a5 --- /dev/null +++ b/.scaffold/aoc/{{ .Project }}/solve.go @@ -0,0 +1,20 @@ +package solutions + +import ( + "github.com/onyx-and-iris/aoc{{ .Scaffold.year }}/{{ .Project }}/internal/one" + "github.com/onyx-and-iris/aoc{{ .Scaffold.year }}/{{ .Project }}/internal/two" +) + +func Solve(buf []byte) (int, int, error) { + answerOne, err := one.Solve(buf) + if err != nil { + return 0, 0, err + } + + answerTwo, err := two.Solve(buf) + if err != nil { + return 0, 0, err + } + + return answerOne, answerTwo, nil +} diff --git a/.scaffold/aoc/{{ .Project }}/solve_internal_test.go b/.scaffold/aoc/{{ .Project }}/solve_internal_test.go new file mode 100644 index 0000000..8c5a300 --- /dev/null +++ b/.scaffold/aoc/{{ .Project }}/solve_internal_test.go @@ -0,0 +1,15 @@ +package solutions + +import ( + _ "embed" + "os" + "testing" +) + +//go:embed testdata/input.txt +var data []byte + +func BenchmarkSolve(b *testing.B) { + os.Stdout, _ = os.Open(os.DevNull) + Solve(data) +} diff --git a/scaffold.sh b/scaffold.sh deleted file mode 100755 index f4bbb36..0000000 --- a/scaffold.sh +++ /dev/null @@ -1,194 +0,0 @@ -#!/usr/bin/env bash - -name=day-"$1" - -if [ -d day-"$name" ]; then - echo "program $name already exists" - exit 1 -fi - -mkdir -p "$name"/cmd/cli/testdata \ - "$name"/testdata "$name"/internal/one/testdata "$name"/internal/two/testdata -touch "$name"/makefile - -cat <> "$name"/makefile -program = $name - -GO = go -SRC_DIR := src -BIN_DIR := bin - -EXE := \$(BIN_DIR)/\$(program) - -.DEFAULT_GOAL := build - -.PHONY: fmt vet build bench clean -fmt: - \$(GO) fmt ./... - -vet: fmt - \$(GO) vet ./... - -build: vet | \$(BIN_DIR) - \$(GO) build -o \$(EXE) ./\$(SRC_DIR) - -bench: - \$(GO) test ./internal/one/ -bench=. > internal/one/benchmark - \$(GO) test ./internal/two/ -bench=. > internal/two/benchmark - \$(GO) test . -count=10 -bench=. > benchmark - -\$(BIN_DIR): - @mkdir -p \$@ - -clean: - @rm -rv \$(BIN_DIR) -EOT - -cat <> "$name"/cmd/cli/main.go -/******************************************************************************** - Advent of Code 2024 - $name -********************************************************************************/ - -package main - -func main() { - -} -EOT - -touch "$name"/solve.go -cat <> "$name"/solve.go -package name - -import ( - "github.com/onyx-and-iris/aoc2024/day-$1/internal/one" - "github.com/onyx-and-iris/aoc2024/day-$1/internal/two" -) - -func Solve(buf []byte) (int, int, error) { - answerOne, err := one.Solve(buf) - if err != nil { - return 0, 0, err - } - - answerTwo, err := two.Solve(buf) - if err != nil { - return 0, 0, err - } - - return answerOne, answerTwo, nil -} -EOT - -touch "$name"/internal/one/solve.go -cat <> "$name"/internal/one/solve.go -package one - -import ( - "bufio" - "bytes" - "io" -) - -func Solve(buf []byte) (int, error) { - r := bytes.NewReader(buf) - err := parseLines(r) - if err != nil { - return 0, err - } - - return 0, nil -} - -func parseLines(r io.Reader) error { - scanner := bufio.NewScanner(r) - for scanner.Scan() { - } - - if err := scanner.Err(); err != nil { - return err - } - - return nil -} -EOT - -touch "$name"/internal/one/solve_internal_test.go -cat <> "$name"/internal/one/solve_internal_test.go -package one - -import ( - _ "embed" - "os" - "testing" -) - -//go:embed testdata/input.txt -var data []byte - -func BenchmarkSolve(b *testing.B) { - os.Stdout, _ = os.Open(os.DevNull) - Solve(data) -} -EOT - - -touch "$name"/internal/two/solve.go -cat <> "$name"/internal/two/solve.go -package two - -import ( - "bufio" - "bytes" - "io" -) - -func Solve(buf []byte) (int, error) { - r := bytes.NewReader(buf) - err := parseLines(r) - if err != nil { - return 0, err - } - - return 0, nil -} - -func parseLines(r io.Reader) error { - scanner := bufio.NewScanner(r) - for scanner.Scan() { - } - - if err := scanner.Err(); err != nil { - return err - } - - return nil -} -EOT - -touch "$name"/internal/two/solve_internal_test.go -cat <> "$name"/internal/two/solve_internal_test.go -package two - -import ( - _ "embed" - "os" - "testing" -) - -//go:embed testdata/input.txt -var data []byte - -func BenchmarkSolve(b *testing.B) { - os.Stdout, _ = os.Open(os.DevNull) - Solve(data) -} -EOT - - -cd "$name" || exit - -go mod init github.com/onyx-and-iris/aoc2024/"$name" - -MKFILES=../mkfiles.sh -[[ -f $MKFILES ]] && ln -s $MKFILES . \ No newline at end of file