mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
modified scripts to use a folder for each day - prevent a huge package "namespace" headache
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user