Add Go template

This commit is contained in:
akp 2020-12-05 14:35:38 +00:00
parent a1f00683bc
commit ac6564d847
No known key found for this signature in database
GPG key ID: D3E7EAA31B39637E
6 changed files with 119 additions and 0 deletions

5
go.mod Normal file
View file

@ -0,0 +1,5 @@
module adventOfCode
go 1.15
require github.com/fatih/color v1.10.0

9
go.sum Normal file
View file

@ -0,0 +1,9 @@
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View file

@ -0,0 +1,5 @@
package challenge
func parse(instr string) []string {
return []string{}
}

View file

@ -0,0 +1,6 @@
package challenge
func PartOne(instr string) int {
// inputSlice := parse(instr)
return 0
}

View file

@ -0,0 +1,6 @@
package challenge
func PartTwo(instr string) int {
// inputSlice := parse(instr)
return 0
}

88
template/go/main.go Normal file
View file

@ -0,0 +1,88 @@
package main
import (
"adventOfCode/template/go/challenge"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"runtime"
"strconv"
"github.com/fatih/color"
)
const (
year = "2020"
day = "1"
title = "Report Repair"
)
func main() {
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()))
var challengeInput string
{
inb, err := ioutil.ReadFile("input.txt")
if err != nil {
fmt.Println("Error: could not open input.txt")
os.Exit(-1)
}
challengeInput = string(inb)
}
runTests()
fmt.Println("Answers")
fmt.Fprintf(color.Output, "Part %s: %s\n", color.BlueString("1"), color.BlueString(strconv.Itoa(challenge.PartOne(challengeInput))))
fmt.Fprintf(color.Output, "Part %s: %s\n", color.BlueString("2"), color.BlueString(strconv.Itoa(challenge.PartTwo(challengeInput))))
}
type tc struct {
Input string `json:"input"`
Expected int `json:"expected"`
}
func runTests() {
testCases := struct {
One []tc `json:"one"`
Two []tc `json:"two"`
}{}
{
inb, err := ioutil.ReadFile("testCases.json")
if err != nil {
fmt.Println("Error: could not open testCases.json. Skipping tests")
return
}
err = json.Unmarshal(inb, &testCases)
if err != nil {
fmt.Println("Error: could not parse testCases.json. Skipping tests")
return
}
}
fmt.Println("Test cases")
rt := func(tcs []tc, f func(string) int, n string) {
for i, tc := range tcs {
fmt.Fprintf(color.Output, "%s ", color.BlueString(n+"."+strconv.Itoa(i+1)))
result := f(tc.Input)
if result == tc.Expected {
fmt.Fprintf(color.Output, "%s", color.GreenString("pass"))
} else {
fmt.Fprintf(color.Output, "%s (got %s, expected %s)", color.RedString("fail"), color.BlueString(strconv.Itoa(result)), color.BlueString(strconv.Itoa(tc.Expected)))
}
fmt.Println()
}
}
rt(testCases.One, challenge.PartOne, "1")
rt(testCases.Two, challenge.PartTwo, "2")
fmt.Println()
}