-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: sol.go file gen by tpl now pure golang
- Loading branch information
v.funtikov
committed
Dec 23, 2021
1 parent
ab09f5f
commit 737a3e8
Showing
3 changed files
with
103 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package day{{.Day}} | ||
|
||
import "aoc" | ||
|
||
/* | ||
Note: output arg type of Part1 and Part2 can be freely changed. | ||
*/ | ||
|
||
func Part1(lines []string) int { | ||
//todo: write code here | ||
return -1 | ||
} | ||
|
||
func Part2(lines []string) int { | ||
//todo: write code here | ||
return -1 | ||
} | ||
|
||
func init() { | ||
// this registers solution to be able to run from ./cmd/main.go, | ||
// BUT you must add import like `_ "aoc/day{{.Day}}"` in "main.go" by hands! | ||
aoc.RegisterSolution({{.Day}}, aoc.DaySolution{ | ||
Part1: func(lines []string) interface{} { | ||
return Part1(lines) | ||
}, | ||
Part2: func(lines []string) interface{} { | ||
return Part2(lines) | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// +build gen | ||
|
||
package main | ||
|
||
import ( | ||
_ "embed" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"text/template" | ||
) | ||
|
||
//go:embed soltpl.gotext | ||
var tpl string | ||
|
||
var errLog = log.New(os.Stderr, "ERR ", log.LstdFlags) | ||
var warnLog = log.New(os.Stderr, "WRN ", log.LstdFlags) | ||
|
||
func main() { | ||
fmt.Println("Solution file generator") | ||
|
||
t, err := template.New("").Parse(tpl) | ||
if err != nil { | ||
errLog.Println("template parse:", err) | ||
os.Exit(1) | ||
} | ||
|
||
var day int | ||
flag.IntVar(&day, "day", 0, "day number (ex: 1)") | ||
flag.Parse() | ||
|
||
if day <= 0 { | ||
errLog.Println("wrong day number") | ||
} | ||
|
||
folder := filepath.Join(".", fmt.Sprintf("day%2d", day)) | ||
|
||
_, err = os.Stat(folder) | ||
if err == nil { | ||
warnLog.Println("folder already exists, wrong day?") | ||
os.Exit(1) | ||
} | ||
|
||
err = os.Mkdir(folder, 0755) | ||
if err != nil { | ||
errLog.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
f, err := os.Create(filepath.Join(folder, "sol.go")) | ||
if err != nil { | ||
errLog.Println("create solution go-file:", err) | ||
os.Exit(1) | ||
} | ||
defer f.Close() | ||
|
||
type data struct { | ||
Day int | ||
} | ||
|
||
err = t.Execute(f, data{Day: day}) | ||
if err != nil { | ||
errLog.Println("process template:", err) | ||
os.Exit(1) | ||
} | ||
|
||
log.Println("folder crated:", folder) | ||
|
||
fmt.Println("Done.") | ||
} |