From dda597a577268bda46e88c7209c74c59eee7b7e3 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 26 Dec 2024 19:00:37 +0000 Subject: [PATCH] add day-25 part1 --- day-25/.gitignore | 1 + day-25/cmd/cli/main.go | 41 ++++++++++++++++ day-25/go.mod | 7 +++ day-25/go.sum | 16 +++++++ day-25/internal/one/schematic.go | 55 ++++++++++++++++++++++ day-25/internal/one/solve.go | 34 +++++++++++++ day-25/internal/one/solve_internal_test.go | 15 ++++++ day-25/internal/one/util.go | 30 ++++++++++++ day-25/makefile | 30 ++++++++++++ day-25/solve.go | 20 ++++++++ day-25/solve_internal_test.go | 15 ++++++ 11 files changed, 264 insertions(+) create mode 100644 day-25/.gitignore create mode 100644 day-25/cmd/cli/main.go create mode 100644 day-25/go.mod create mode 100644 day-25/go.sum create mode 100644 day-25/internal/one/schematic.go create mode 100644 day-25/internal/one/solve.go create mode 100644 day-25/internal/one/solve_internal_test.go create mode 100644 day-25/internal/one/util.go create mode 100644 day-25/makefile create mode 100644 day-25/solve.go create mode 100644 day-25/solve_internal_test.go diff --git a/day-25/.gitignore b/day-25/.gitignore new file mode 100644 index 0000000..780f6b0 --- /dev/null +++ b/day-25/.gitignore @@ -0,0 +1 @@ +internal/two \ No newline at end of file diff --git a/day-25/cmd/cli/main.go b/day-25/cmd/cli/main.go new file mode 100644 index 0000000..d5c4990 --- /dev/null +++ b/day-25/cmd/cli/main.go @@ -0,0 +1,41 @@ +/******************************************************************************** + Advent of Code 2024 - day-25 +********************************************************************************/ + +package main + +import ( + "embed" + "flag" + "fmt" + "slices" + + log "github.com/sirupsen/logrus" + + problems "github.com/onyx-and-iris/aoc2024/day-25" +) + +//go:embed testdata +var files embed.FS + +func main() { + filename := flag.String("f", "input.txt", "input file") + loglevel := flag.Int("l", int(log.InfoLevel), "log level") + flag.Parse() + + if slices.Contains(log.AllLevels, log.Level(*loglevel)) { + log.SetLevel(log.Level(*loglevel)) + } + + data, err := files.ReadFile(fmt.Sprintf("testdata/%s", *filename)) + if err != nil { + log.Fatal(err) + } + + one, two, err := problems.Solve(data) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("solution one: %d\nsolution two: %d\n", one, two) +} diff --git a/day-25/go.mod b/day-25/go.mod new file mode 100644 index 0000000..16d2bb2 --- /dev/null +++ b/day-25/go.mod @@ -0,0 +1,7 @@ +module github.com/onyx-and-iris/aoc2024/day-25 + +go 1.23.3 + +require github.com/sirupsen/logrus v1.9.3 + +require golang.org/x/sys v0.12.0 // indirect diff --git a/day-25/go.sum b/day-25/go.sum new file mode 100644 index 0000000..8d5c188 --- /dev/null +++ b/day-25/go.sum @@ -0,0 +1,16 @@ +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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/day-25/internal/one/schematic.go b/day-25/internal/one/schematic.go new file mode 100644 index 0000000..89a47ac --- /dev/null +++ b/day-25/internal/one/schematic.go @@ -0,0 +1,55 @@ +package one + +import ( + "strings" +) + +type kindOfSchematic int + +const ( + Lock kindOfSchematic = iota + Key +) + +type schematic struct { + kind kindOfSchematic + heights []int + data []string +} + +func newSchematic(buf [][]byte) *schematic { + var kind kindOfSchematic + data := make([]string, len(buf)) + heights := make([]int, len(buf[0])) + + for i, line := range buf { + data[i] = string(line) + + if i == 0 { + if allInString(data[i], '#') { + kind = Lock + } else if allInString(data[i], '.') { + kind = Key + } + } + + if kind == Lock && i == 0 { + continue + } + if kind == Key && i == len(buf)-1 { + continue + } + + for j, r := range data[i] { + if r == '#' { + heights[j]++ + } + } + } + + return &schematic{kind, heights, data} +} + +func (s *schematic) String() string { + return strings.Join(s.data, "\n") +} diff --git a/day-25/internal/one/solve.go b/day-25/internal/one/solve.go new file mode 100644 index 0000000..9f0f5f0 --- /dev/null +++ b/day-25/internal/one/solve.go @@ -0,0 +1,34 @@ +package one + +import ( + "bytes" + + log "github.com/sirupsen/logrus" +) + +func Solve(buf []byte) (int, error) { + r := bytes.NewReader(buf) + maxHeight, schematics, err := parseLines(r) + if err != nil { + return 0, err + } + + var count int + for _, lock := range schematics[Lock] { + for _, key := range schematics[Key] { + if func() bool { + for i := range key.heights { + if key.heights[i]+lock.heights[i] > maxHeight { + return false + } + } + return true + }() { + count++ + log.Debugf("lock: %v and key: %v", lock.heights, key.heights) + } + } + } + + return count, nil +} diff --git a/day-25/internal/one/solve_internal_test.go b/day-25/internal/one/solve_internal_test.go new file mode 100644 index 0000000..e5fd7ea --- /dev/null +++ b/day-25/internal/one/solve_internal_test.go @@ -0,0 +1,15 @@ +package one + +import ( + _ "embed" + "os" + "testing" +) + +//go:embed testdata/input.txt +var data []byte + +func BenchmarkSolve(b *testing.B) { + os.Stdout, _ = os.Open(os.DevNull) + Solve(data) +} diff --git a/day-25/internal/one/util.go b/day-25/internal/one/util.go new file mode 100644 index 0000000..351f497 --- /dev/null +++ b/day-25/internal/one/util.go @@ -0,0 +1,30 @@ +package one + +import ( + "bytes" + "io" +) + +func parseLines(r io.Reader) (int, map[kindOfSchematic][]*schematic, error) { + raw, _ := io.ReadAll(r) + rawSchematics := bytes.Split(raw, []byte("\n\n")) + + schematics := map[kindOfSchematic][]*schematic{Lock: {}, Key: {}} + for _, rawSchematic := range rawSchematics { + s := newSchematic(bytes.Split(rawSchematic, []byte("\n"))) + schematics[s.kind] = append(schematics[s.kind], s) + } + + maxHeight := len(schematics[Lock][0].heights) + + return maxHeight, schematics, nil +} + +func allInString(s string, r rune) bool { + for _, c := range s { + if c != r { + return false + } + } + return true +} diff --git a/day-25/makefile b/day-25/makefile new file mode 100644 index 0000000..690da36 --- /dev/null +++ b/day-25/makefile @@ -0,0 +1,30 @@ +program = day-25 + +GO = go +SRC_DIR := src +BIN_DIR := bin + +EXE := $(BIN_DIR)/$(program) + +.DEFAULT_GOAL := build + +.PHONY: fmt vet build bench clean +fmt: + $(GO) fmt ./... + +vet: fmt + $(GO) vet ./... + +build: vet | $(BIN_DIR) + $(GO) build -o $(EXE) ./$(SRC_DIR) + +bench: + $(GO) test ./internal/one/ -bench=. > internal/one/benchmark + $(GO) test ./internal/two/ -bench=. > internal/two/benchmark + $(GO) test . -count=10 -bench=. > benchmark + +$(BIN_DIR): + @mkdir -p $@ + +clean: + @rm -rv $(BIN_DIR) diff --git a/day-25/solve.go b/day-25/solve.go new file mode 100644 index 0000000..bbdc15f --- /dev/null +++ b/day-25/solve.go @@ -0,0 +1,20 @@ +package daytwentyfive + +import ( + "github.com/onyx-and-iris/aoc2024/day-25/internal/one" + "github.com/onyx-and-iris/aoc2024/day-25/internal/two" +) + +func Solve(buf []byte) (int, int, error) { + answerOne, err := one.Solve(buf) + if err != nil { + return 0, 0, err + } + + answerTwo, err := two.Solve(buf) + if err != nil { + return 0, 0, err + } + + return answerOne, answerTwo, nil +} diff --git a/day-25/solve_internal_test.go b/day-25/solve_internal_test.go new file mode 100644 index 0000000..6a01921 --- /dev/null +++ b/day-25/solve_internal_test.go @@ -0,0 +1,15 @@ +package daytwentyfive + +import ( + _ "embed" + "os" + "testing" +) + +//go:embed testdata/input.txt +var data []byte + +func BenchmarkSolve(b *testing.B) { + os.Stdout, _ = os.Open(os.DevNull) + Solve(data) +}