Update template
This commit is contained in:
parent
49277ee1b5
commit
313e74738c
4 changed files with 76 additions and 45 deletions
|
@ -19,6 +19,26 @@ const (
|
|||
)
|
||||
|
||||
func main() {
|
||||
var infoStruct info
|
||||
{
|
||||
inb, err := ioutil.ReadFile("info.json")
|
||||
if err != nil {
|
||||
fmt.Println("Error: could not open info.json")
|
||||
os.Exit(-1)
|
||||
}
|
||||
err = json.Unmarshal(inb, &infoStruct)
|
||||
if err != nil {
|
||||
fmt.Println("Error: could not parse info.json")
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
year = infoStruct.Year
|
||||
day = infoStruct.Day
|
||||
title = infoStruct.Title
|
||||
)
|
||||
|
||||
fmt.Fprintf(color.Output, "%s: day %s - %s\n", color.YellowString("AoC "+year), color.BlueString(day), title)
|
||||
fmt.Fprintf(color.Output, "Go %s\n\n", color.BlueString(runtime.Version()))
|
||||
|
||||
|
@ -32,7 +52,7 @@ func main() {
|
|||
challengeInput = string(inb)
|
||||
}
|
||||
|
||||
runTests()
|
||||
runTests(infoStruct)
|
||||
|
||||
fmt.Println("Answers")
|
||||
fmt.Fprintf(color.Output, "Part %s: %s\n", color.BlueString("1"), color.BlueString(strconv.Itoa(challenge.PartOne(challengeInput))))
|
||||
|
@ -45,26 +65,22 @@ type tc struct {
|
|||
Expected int `json:"expected"`
|
||||
}
|
||||
|
||||
func runTests() {
|
||||
|
||||
testCases := struct {
|
||||
type info struct {
|
||||
Year string `json:"year"`
|
||||
Day string `json:"day"`
|
||||
Title string `json:"title"`
|
||||
TestCases struct {
|
||||
One []tc `json:"one"`
|
||||
Two []tc `json:"two"`
|
||||
}{}
|
||||
} `json:"testCases"`
|
||||
}
|
||||
|
||||
{
|
||||
inb, err := ioutil.ReadFile("testCases.json")
|
||||
if err != nil {
|
||||
fmt.Println("Info: could not open testCases.json. Skipping tests")
|
||||
fmt.Println()
|
||||
return
|
||||
}
|
||||
err = json.Unmarshal(inb, &testCases)
|
||||
if err != nil {
|
||||
fmt.Println("Error: could not parse testCases.json. Skipping tests")
|
||||
fmt.Println()
|
||||
return
|
||||
}
|
||||
func runTests(infoStruct info) {
|
||||
|
||||
if len(infoStruct.TestCases.One) == 0 && len(infoStruct.TestCases.Two) == 0 {
|
||||
fmt.Println("Info: no test cases specified. Skipping tests")
|
||||
fmt.Println()
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Test cases")
|
||||
|
@ -82,8 +98,8 @@ func runTests() {
|
|||
}
|
||||
}
|
||||
|
||||
rt(testCases.One, challenge.PartOne, "1")
|
||||
rt(testCases.Two, challenge.PartTwo, "2")
|
||||
rt(infoStruct.TestCases.One, challenge.PartOne, "1")
|
||||
rt(infoStruct.TestCases.Two, challenge.PartTwo, "2")
|
||||
|
||||
fmt.Println()
|
||||
|
||||
|
|
19
template/info.json
Normal file
19
template/info.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"year": "2020",
|
||||
"day": "1",
|
||||
"title": "Report Repair",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "1721\n979\n366\n299\n675\n1456",
|
||||
"expected": 514579
|
||||
}
|
||||
],
|
||||
"two": [
|
||||
{
|
||||
"input": "1721\n979\n366\n299\n675\n1456",
|
||||
"expected": 241861950
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
|
@ -7,21 +7,19 @@ from rich import print
|
|||
from partOne import partOne
|
||||
from partTwo import partTwo
|
||||
|
||||
year = "2020"
|
||||
day = "1"
|
||||
title = "Report Repair"
|
||||
def run_tests(test_cases):
|
||||
do_tests = True
|
||||
if len(test_cases) == 0:
|
||||
do_tests = False
|
||||
elif len(test_cases["one"]) == 0 and len(test_cases["two"]) == 0:
|
||||
do_tests = False
|
||||
|
||||
def run_tests():
|
||||
try:
|
||||
test_cases = open("testCases.json").read()
|
||||
except FileNotFoundError:
|
||||
print("Info: could not open testCases.json. Skipping tests\n")
|
||||
if not do_tests:
|
||||
print("Info: no test cases specified. Skipping tests\n")
|
||||
return
|
||||
|
||||
print("Test cases")
|
||||
|
||||
test_cases = json.loads(test_cases)
|
||||
|
||||
def rt(tcs, f, n):
|
||||
for i, tc in enumerate(tcs):
|
||||
print(f"{n}.{i+1} ", end="")
|
||||
|
@ -38,6 +36,18 @@ def run_tests():
|
|||
print()
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
info = open("info.json").read()
|
||||
except FileNotFoundError:
|
||||
print("Error: could not open info.json")
|
||||
sys.exit(-1)
|
||||
|
||||
info = json.loads(info)
|
||||
|
||||
year = info["year"]
|
||||
day = info["day"]
|
||||
title = info["title"]
|
||||
|
||||
print(f"[yellow]AoC {year}[/yellow]: day {day} - {title}")
|
||||
print(f"Python {platform.python_version()}\n")
|
||||
|
||||
|
@ -47,7 +57,7 @@ if __name__ == "__main__":
|
|||
print("Error: could not open input.txt")
|
||||
sys.exit(-1)
|
||||
|
||||
run_tests()
|
||||
run_tests(info["testCases"])
|
||||
|
||||
print("Answers")
|
||||
print("Part 1:", partOne(challenge_input))
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
{
|
||||
"one": [
|
||||
{
|
||||
"input": "1721\n979\n366\n299\n675\n1456",
|
||||
"expected": 514579
|
||||
}
|
||||
],
|
||||
"two": [
|
||||
{
|
||||
"input": "1721\n979\n366\n299\n675\n1456",
|
||||
"expected": 241861950
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue