aoc2024/scaffold.sh

194 lines
3.0 KiB
Bash
Raw Normal View History

2024-12-01 17:50:02 +00:00
#!/usr/bin/env bash
name=day-"$1"
if [ -d day-"$name" ]; then
echo "program $name already exists"
exit 1
fi
2024-12-02 02:06:35 +00:00
mkdir -p "$name"/cmd/cli/testdata \
"$name"/testdata "$name"/internal/one/testdata "$name"/internal/two/testdata
2024-12-01 17:50:02 +00:00
touch "$name"/makefile
cat <<EOT >> "$name"/makefile
program = $name
GO = go
SRC_DIR := src
BIN_DIR := bin
EXE := \$(BIN_DIR)/\$(program)
.DEFAULT_GOAL := build
2024-12-01 21:14:30 +00:00
.PHONY: fmt vet build bench clean
2024-12-01 17:50:02 +00:00
fmt:
\$(GO) fmt ./...
vet: fmt
\$(GO) vet ./...
build: vet | \$(BIN_DIR)
\$(GO) build -o \$(EXE) ./\$(SRC_DIR)
2024-12-01 21:14:30 +00:00
bench:
2024-12-02 02:06:35 +00:00
\$(GO) test ./internal/one/ -bench=. > internal/one/benchmark
\$(GO) test ./internal/two/ -bench=. > internal/two/benchmark
\$(GO) test . -count=10 -bench=. > benchmark
2024-12-01 21:14:30 +00:00
2024-12-01 17:50:02 +00:00
\$(BIN_DIR):
@mkdir -p \$@
clean:
@rm -rv \$(BIN_DIR)
EOT
2024-12-02 02:06:35 +00:00
cat <<EOT >> "$name"/cmd/cli/main.go
2024-12-01 17:50:02 +00:00
/********************************************************************************
Advent of Code 2024 - $name
********************************************************************************/
package main
func main() {
}
EOT
2024-12-03 14:42:35 +00:00
touch "$name"/solve.go
2024-12-05 18:24:29 +00:00
cat <<EOT >> "$name"/solve.go
2024-12-03 14:42:35 +00:00
package name
2024-12-05 18:24:29 +00:00
import (
"github.com/onyx-and-iris/aoc2024/day-$1/internal/one"
"github.com/onyx-and-iris/aoc2024/day-$1/internal/two"
)
2024-12-06 19:57:07 +00:00
func Solve(buf []byte) (int, int, error) {
answerOne, err := one.Solve(buf)
2024-12-05 18:24:29 +00:00
if err != nil {
return 0, 0, err
}
2024-12-06 19:57:07 +00:00
answerTwo, err := two.Solve(buf)
2024-12-05 18:24:29 +00:00
if err != nil {
return 0, 0, err
}
return answerOne, answerTwo, nil
}
EOT
touch "$name"/internal/one/solve.go
cat <<EOT >> "$name"/internal/one/solve.go
package one
import (
"bufio"
"bytes"
"io"
)
2024-12-06 19:57:07 +00:00
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
2024-12-05 18:24:29 +00:00
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 <<EOT >> "$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)
2024-12-03 14:42:35 +00:00
}
EOT
2024-12-05 18:24:29 +00:00
touch "$name"/internal/two/solve.go
cat <<EOT >> "$name"/internal/two/solve.go
package two
import (
"bufio"
"bytes"
"io"
)
2024-12-06 19:57:07 +00:00
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
2024-12-05 18:24:29 +00:00
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 <<EOT >> "$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
2024-12-01 17:50:02 +00:00
cd "$name" || exit
2024-12-03 14:42:35 +00:00
go mod init github.com/onyx-and-iris/aoc2024/"$name"
MKFILES=../mkfiles.sh
[[ -f $MKFILES ]] && ln -s $MKFILES .