From 045d73b069e59cf9f53a1d24401af50ed0519f9a Mon Sep 17 00:00:00 2001 From: alexchao26 Date: Fri, 27 Nov 2020 14:27:43 -0500 Subject: [PATCH] modified scripts to use a folder for each day - prevent a huge package "namespace" headache --- 2020/README.md | 5 +-- README.md | 3 +- scripts/fetchers/cmd-inputs/inputs.go | 2 +- scripts/fetchers/cmd-prompts/prompts.go | 4 +-- scripts/fetchers/fetchers.go | 12 ++++++- scripts/template/template.go | 45 +++++++++++++++++++------ 6 files changed, 53 insertions(+), 18 deletions(-) diff --git a/2020/README.md b/2020/README.md index 391bd1d..e2bd167 100644 --- a/2020/README.md +++ b/2020/README.md @@ -1,5 +1,6 @@ ## 2020 Event -To run days for 2020, I'm planning on using `go test -run RegExpToMatchFunctionNames .` to make it cleaner to run the examples and inputs and not requiring 189203829048024 files for main packages +To run days for 2020, I'm planning on using `go test -run RegExpToMatchFunctionNames .` to make it cleaner to run any given examples. +`go run main.go -part <1 or 2>` will be usable to run the actual inputs for that day -Scripts have also been added to the outer folder to setup files for a particular day more easily +Scripts have been added to the outer folder to setup files for a particular day more easily diff --git a/README.md b/README.md index e366516..746d563 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,8 @@ ### Scripts Addition for 2020 event. -Note: need to make folder for the year first (`mkdir 2020`) path|usage ---|--- scripts/fetchers/cmd-inputs | go run scripts/fetches/cmd-inputs/inputs.go
-cookie flag for session cookie from AOC website (required to be set via flag OR on ENV)
-day flag defaults to today
-year flag defaults to current year scripts/fetchers/cmd-prompts | go run scripts/fetches/cmd-prompts/prompts.go
same flags as inputs script -scripts/template/template.go | go run scripts/template/template.go
same -day and -year flags, quickly templates up files for the given day's solutions.
Note that functions are meant to be run using `go test -run DayX .` from 2020 folder +scripts/template/template.go | go run scripts/template/template.go
same -day and -year flags, quickly templates up files for the given day's solutions.
Note that functions can be run using `go test -run PartX .` or via `go run main.go -part <1 or 2>` diff --git a/scripts/fetchers/cmd-inputs/inputs.go b/scripts/fetchers/cmd-inputs/inputs.go index a92dcf1..247e8ed 100644 --- a/scripts/fetchers/cmd-inputs/inputs.go +++ b/scripts/fetchers/cmd-inputs/inputs.go @@ -18,7 +18,7 @@ func main() { body := fetchers.GetWithAOCCookie(url, cookie) // write to file - filename := filepath.Join(util.Dirname(), "../../..", fmt.Sprintf("%d/day%02d-input.txt", year, day)) + filename := filepath.Join(util.Dirname(), "../../..", fmt.Sprintf("%d/day%02d/input.txt", year, day)) fetchers.WriteToFile(filename, body) fmt.Println("Wrote to file: ", filename) diff --git a/scripts/fetchers/cmd-prompts/prompts.go b/scripts/fetchers/cmd-prompts/prompts.go index 2a67b54..8ec4134 100644 --- a/scripts/fetchers/cmd-prompts/prompts.go +++ b/scripts/fetchers/cmd-prompts/prompts.go @@ -25,7 +25,7 @@ func main() { prompt := parseHTML(body) // write to file - filename := filepath.Join(util.Dirname(), "../../../", fmt.Sprintf("%d/day%02d-prompt.md", year, day)) + filename := filepath.Join(util.Dirname(), "../../../", fmt.Sprintf("%d/day%02d/prompt.md", year, day)) fetchers.WriteToFile(filename, []byte(prompt)) fmt.Println("Wrote prompt to file: ", filename) @@ -66,7 +66,7 @@ func dfsHTML(node *html.Node, cb func(*html.Node) []interface{}) []interface{} { func cbFindDayDescClass(node *html.Node) []interface{} { for _, attr := range node.Attr { if attr.Key == "class" && attr.Val == "day-desc" { - fmt.Println("day-desc node found!") + // fmt.Println("day-desc node found!") return []interface{}{node} } } diff --git a/scripts/fetchers/fetchers.go b/scripts/fetchers/fetchers.go index 7e612b7..693abd6 100644 --- a/scripts/fetchers/fetchers.go +++ b/scripts/fetchers/fetchers.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net/http" "os" + "path/filepath" "strings" "time" ) @@ -55,15 +56,24 @@ func GetWithAOCCookie(url string, cookie string) []byte { fmt.Println("response length is", len(body)) if strings.HasPrefix(string(body), "Please don't repeatedly") { - panic("Repeated request github.com/alexchao26/advent-of-code-go error") + // panic("Repeated request github.com/alexchao26/advent-of-code-go error") } return body } func WriteToFile(filename string, contents []byte) { + MakeDir(filepath.Dir(filename)) + err := ioutil.WriteFile(filename, contents, os.ModePerm) if err != nil { panicWrap(err, "writing file") } } + +func MakeDir(dir string) { + err := os.MkdirAll(dir, os.ModePerm) + if err != nil { + panic(err) + } +} diff --git a/scripts/template/template.go b/scripts/template/template.go index 188341d..38087c1 100644 --- a/scripts/template/template.go +++ b/scripts/template/template.go @@ -15,7 +15,7 @@ type TemplateData struct { Day string // a string to include the prefixing zero } -var testTemplateString = `package aoc{{.Year}} +var testTemplateString = `package main import ( "testing" @@ -23,24 +23,47 @@ import ( "github.com/alexchao26/advent-of-code-go/util" ) -func TestDay{{.Day}}Part1(t *testing.T) { +func TestPart1(t *testing.T) { + // Examples + // Run actual problem input - day{{.Day}}Part1(util.ReadFile("./day{{.Day}}-input.txt")) + // part1(util.ReadFile("input.txt")) } -func TestDay{{.Day}}Part2(t *testing.T) { +func TestPart2(t *testing.T) { + // Examples + // Run actual problem input - day{{.Day}}Part2(util.ReadFile("./day{{.Day}}-input.txt")) + // part2(util.ReadFile("input.txt")) } ` -var solutionTemplateString = `package aoc{{.Year}} +var solutionTemplateString = `package main -func day{{.Day}}Part1(input string) int { +import ( + "flag" + "fmt" + + "github.com/alexchao26/advent-of-code-go/util" +) + +func main() { + var part int + flag.IntVar(&part, "part", 1, "part 1 or 2") + fmt.Println("Running part", part) + + if part == 1 { + part1(util.ReadFile("./input.txt")) + } else { + part2(util.ReadFile("./input.txt")) + } +} + +func part1(input string) int { return 0 } -func day{{.Day}}Part2(input string) int { +func part2(input string) int { return 0 } ` @@ -61,8 +84,10 @@ func main() { panic(err) } - solutionFilename := filepath.Join(util.Dirname(), "../../", fmt.Sprintf("%d/day%02d.go", year, day)) - testFilename := filepath.Join(util.Dirname(), "../../", fmt.Sprintf("%d/day%02d_test.go", year, day)) + solutionFilename := filepath.Join(util.Dirname(), "../../", fmt.Sprintf("%d/day%02d/main.go", year, day)) + testFilename := filepath.Join(util.Dirname(), "../../", fmt.Sprintf("%d/day%02d/main_test.go", year, day)) + + fetchers.MakeDir(filepath.Dir(solutionFilename)) EnsureNotOverwriting(solutionFilename) EnsureNotOverwriting(testFilename)