diff options
| author | Mike Vink <mike1994vink@gmail.com> | 2023-02-25 17:50:50 +0100 |
|---|---|---|
| committer | Mike Vink <mike1994vink@gmail.com> | 2023-02-25 17:50:50 +0100 |
| commit | 9dbdf08cb814a32ebb27d200391af83c6f2a7ff5 (patch) | |
| tree | 6fff3e789cdea402113e8705edb739c26dd973f2 | |
| parent | cc446240fac48c545c522f3d69c5d04a93b63461 (diff) | |
start scaffolding and boilerplate
| -rw-r--r-- | 2022/01/day1.txt (renamed from day1.txt) | 0 | ||||
| -rw-r--r-- | 2022/01/main.go (renamed from day1.go) | 11 | ||||
| -rw-r--r-- | aoc/aoc_suite_test.go | 13 | ||||
| -rw-r--r-- | aoc/day.go | 19 | ||||
| -rw-r--r-- | aoc/inputters.go | 42 | ||||
| -rw-r--r-- | aoc/inputters_test.go | 53 | ||||
| -rw-r--r-- | aoc/scanner.go | 15 | ||||
| -rw-r--r-- | flake.nix | 1 | ||||
| -rw-r--r-- | go.mod | 17 | ||||
| -rw-r--r-- | go.sum | 41 | ||||
| -rw-r--r-- | main.go | 36 |
11 files changed, 241 insertions, 7 deletions
diff --git a/day1.txt b/2022/01/day1.txt index 78ccff4..78ccff4 100644 --- a/day1.txt +++ b/2022/01/day1.txt diff --git a/day1.go b/2022/01/main.go index 1b2fc29..96378a6 100644 --- a/day1.go +++ b/2022/01/main.go @@ -3,8 +3,9 @@ package main import ( "bufio" "fmt" - "os" "strconv" + + "mvinkio.online/aoc/aoc" ) func biggestElf(data []int) []int { @@ -44,13 +45,9 @@ func sum(array []int) int { return s } -// Should've kept part1 solution like in day3... +// Boilerplate func main() { - f, err := os.Open("day1.txt") - if err != nil { - fmt.Println(err) - } - defer f.Close() + s := aoc.NewScannerFromFile("2022/01/input.txt") data := make([]int, 0) scanner := bufio.NewScanner(f) diff --git a/aoc/aoc_suite_test.go b/aoc/aoc_suite_test.go new file mode 100644 index 0000000..6e3b38b --- /dev/null +++ b/aoc/aoc_suite_test.go @@ -0,0 +1,13 @@ +package aoc_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestAoc(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Aoc Suite") +} diff --git a/aoc/day.go b/aoc/day.go new file mode 100644 index 0000000..7505223 --- /dev/null +++ b/aoc/day.go @@ -0,0 +1,19 @@ +package aoc + +import ( + "bufio" + "context" +) + +type Reader[T any] interface { + Read(ctx context.Context, scanner *bufio.Scanner) (T, error) +} + +type Solver[T any, R any] interface { + Solve(ctx context.Context, data T) (R, error) +} + +func RunDay[T any, R any](ctx context.Context, in Reader[T], solvers ...Solver[T, R]) { + ctx, cancel := context.WithCancel(ctx) + defer cancel() +} diff --git a/aoc/inputters.go b/aoc/inputters.go new file mode 100644 index 0000000..f677177 --- /dev/null +++ b/aoc/inputters.go @@ -0,0 +1,42 @@ +package aoc + +import ( + "bufio" + "context" + "strconv" +) + +// Read all lines into type T +type ReadLines[T any] func(lines []string) (T, error) + +func (l ReadLines[T]) Read(ctx context.Context, scanner *bufio.Scanner) (T, error) { + lines := []string{} + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + return l(lines) +} + +// Read line by line into type T, collect in []T +type ReadByLine[T any] func(line string) (T, error) + +func (l ReadByLine[T]) Read(ctx context.Context, scanner *bufio.Scanner) ([]T, error) { + lines := []T{} + for scanner.Scan() { + if t, err := l(scanner.Text()); err == nil { + lines = append(lines, t) + } else { + return nil, err + } + } + return lines, nil +} + +// Read lines into ints +var ReadInts = ReadByLine[int](func(line string) (int, error) { + if i, err := strconv.Atoi(line); err != nil { + return 0, err + } else { + return i, nil + } +}) diff --git a/aoc/inputters_test.go b/aoc/inputters_test.go new file mode 100644 index 0000000..2230b1e --- /dev/null +++ b/aoc/inputters_test.go @@ -0,0 +1,53 @@ +package aoc_test + +import ( + "bufio" + "context" + "strconv" + "strings" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "mvinkio.online/aoc/aoc" +) + +func newScanner(input string) *bufio.Scanner { + return bufio.NewScanner(strings.NewReader(input)) +} + +var _ = Describe("Inputters", func() { + Describe("Bylines Inputter", func() { + in := aoc.ReadByLine[int](func(line string) (int, error) { + if i, err := strconv.Atoi(line); err != nil { + return 0, err + } else { + return i, nil + } + }) + It("should map each line in the input to the expected type", func() { + data, err := in.Read(context.TODO(), newScanner("1\n1\n8\n3\n5\n8")) + Expect(err).To(BeNil()) + Expect(data).To(Equal([]int{1, 1, 8, 3, 5, 8})) + }) + }) + + Describe("Lines Inputter", func() { + in := aoc.ReadLines[[]int](func(lines []string) ([]int, error) { + ints := make([]int, len(lines)) + for i, l := range lines { + t, err := strconv.Atoi(l) + if err != nil { + return nil, err + } + ints[i] = t + } + return ints, nil + }) + It("Should correct map to type given all lines", func() { + data, err := in.Read(context.TODO(), newScanner("1\n1\n8\n3\n5\n8")) + Expect(err).To(BeNil()) + Expect(data).To(Equal([]int{1, 1, 8, 3, 5, 8})) + }) + }) +}) diff --git a/aoc/scanner.go b/aoc/scanner.go new file mode 100644 index 0000000..733f49a --- /dev/null +++ b/aoc/scanner.go @@ -0,0 +1,15 @@ +package aoc + +import ( + "bufio" + "log" + "os" +) + +func NewScannerFromFile(filename string) *bufio.Scanner { + f, err := os.Open(filename) + if err != nil { + log.Panic(err) + } + return bufio.NewScanner(f) +} @@ -15,6 +15,7 @@ nativeBuildInputs = [pkgs.bashInteractive]; buildInputs = with pkgs; [ go_1_19 + ginkgo gotests gofumpt gotools @@ -1,3 +1,20 @@ module mvinkio.online/aoc go 1.19 + +require ( + github.com/onsi/ginkgo/v2 v2.8.3 + github.com/onsi/gomega v1.27.1 +) + +require ( + github.com/go-logr/logr v1.2.3 // indirect + github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect + github.com/google/go-cmp v0.5.9 // indirect + github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect + golang.org/x/net v0.7.0 // indirect + golang.org/x/sys v0.5.0 // indirect + golang.org/x/text v0.7.0 // indirect + golang.org/x/tools v0.6.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) @@ -0,0 +1,41 @@ +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE= +github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/onsi/ginkgo/v2 v2.8.3 h1:RpbK1G8nWPNaCVFBWsOGnEQQGgASi6b8fxcWBvDYjxQ= +github.com/onsi/ginkgo/v2 v2.8.3/go.mod h1:6OaUA8BCi0aZfmzYT/q9AacwTzDpNbxILUT+TlBq6MY= +github.com/onsi/gomega v1.27.1 h1:rfztXRbg6nv/5f+Raen9RcGoSecHIFgBBLQK3Wdj754= +github.com/onsi/gomega v1.27.1/go.mod h1:aHX5xOykVYzWOV4WqQy0sy8BQptgukenXpCXfadcIAw= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= @@ -0,0 +1,36 @@ +package main + +import "fmt" + +type flyer interface { + fly() string + dive() string +} + +type printer func(s string) string + +func (p printer) fly() string { + return p("I'm flying!") +} + +func (p printer) dive() string { + return p("I'm diving!") +} + +func printAndReturn(s string) string { + fmt.Println(s) + return s +} + +type bird struct { + flyer +} + +func main() { + bf := bird{ + flyer: printer(printAndReturn), + } + bf.fly() + + bs.fly() +} |
