Started 2022 with go
This commit is contained in:
52
2022/go/template/answer.go
Normal file
52
2022/go/template/answer.go
Normal file
@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Input struct{}
|
||||
|
||||
func input(filename string) *Input {
|
||||
file, err := os.Open(filename)
|
||||
check(err, "Couldn't open %q", filename)
|
||||
defer file.Close()
|
||||
|
||||
input := &Input{}
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
scanner.Split(bufio.ScanLines)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
}
|
||||
|
||||
return input
|
||||
}
|
||||
|
||||
func result(inp *Input, part int8) string {
|
||||
var res string
|
||||
|
||||
if part == 1 {
|
||||
} else {
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func check(e error, msg string, vals ...any) {
|
||||
if e != nil {
|
||||
log.Printf("ERROR: "+msg, vals)
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
result := result(input("sample1"), 1)
|
||||
|
||||
log.Println(result)
|
||||
fmt.Println(result)
|
||||
}
|
38
2022/go/template/answer_test.go
Normal file
38
2022/go/template/answer_test.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInputSample1(t *testing.T) {
|
||||
filename := "sample1"
|
||||
expected := &Input{}
|
||||
var got *Input = input(filename)
|
||||
|
||||
if !reflect.DeepEqual(expected, got) {
|
||||
t.Errorf("input(%q) = %v, want %v", filename, got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResult(t *testing.T) {
|
||||
tests := []struct {
|
||||
part int8
|
||||
filename string
|
||||
expected string
|
||||
}{
|
||||
{1, "sample1", ""},
|
||||
{1, "input", ""},
|
||||
|
||||
// {2, "sample1", ""},
|
||||
// {2, "input", ""},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
var got string = result(input(test.filename), test.part)
|
||||
|
||||
if got != test.expected {
|
||||
t.Errorf("result = %q, want %q", got, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
3
2022/go/template/go.mod
Normal file
3
2022/go/template/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module aoc/template
|
||||
|
||||
go 1.20
|
Reference in New Issue
Block a user