Advent codebase
There's a couple of 'needlessly' cool things I did for advent. The entire project is somewhat production-ized. There's a makefile, tests run in github ci. With the command line, you can generate files for code needed to run, you can run the code against problem input, you can run against tests.
package main
import (
"flag"
"fmt"
"github.com/xxxx/advent/internal"
"github.com/xxxx/advent/problems"
)
func main() {
day := flag.Int("day", 1, "day value, default 1")
part := flag.Int("part", 1, "a part value, default 1")
generate := flag.Bool("generate", false, "set as true to generate dayxx.go, dayxx.in, dayxx_test.go files, default false")
flag.Parse()
if *generate {
internal.GenerateFiles(*day)
} else {
answer, _ := internal.Call(problems.Dailies, internal.GetFunc(*day), *part, "problems/"+internal.GetFile(*day))
fmt.Println(answer[0])
}
}
code generation:
package internal
import (
"fmt"
"os"
"text/template"
)
func generate_go(day_int int) {
day := GetDayString(day_int)
vars := make(map[string]interface{})
vars["day"] = day
go_file := fmt.Sprintf("problems/day%s.go", day)
go_template, err := template.ParseFiles("templates/go.tmpl")
if err != nil {
panic(err)
}
file, err := os.Create(go_file)
if err != nil {
panic(err)
}
defer file.Close()
go_template.Execute(file, vars)
}
func generate_in(day_int int) {
day := GetDayString(day_int)
go_file := fmt.Sprintf("problems/day%s.in", day)
go_template, err := template.ParseFiles("templates/in.tmpl")
if err != nil {
panic(err)
}
file, err := os.Create(go_file)
if err != nil {
panic(err)
}
defer file.Close()
go_template.Execute(file, nil)
}
func generate_test(day_int int) {
day := GetDayString(day_int)
vars := make(map[string]interface{})
vars["day"] = day
go_file := fmt.Sprintf("problems/day%s_test.go", day)
go_template, err := template.ParseFiles("templates/test.tmpl")
if err != nil {
panic(err)
}
file, err := os.Create(go_file)
if err != nil {
panic(err)
}
defer file.Close()
go_template.Execute(file, vars)
}
func GenerateFiles(day_int int) {
generate_go(day_int)
generate_in(day_int)
generate_test(day_int)
}