From ef120d05d4ea9a085e4898cf24ebf3fb74d1c25b Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Mon, 23 Dec 2024 19:56:13 +0000 Subject: [PATCH] add day-23 + benchmarks --- day-23/benchmark | 15 ++++ day-23/cmd/cli/main.go | 41 +++++++++++ day-23/go.mod | 7 ++ day-23/go.sum | 15 ++++ day-23/internal/one/benchmark | 6 ++ day-23/internal/one/solve.go | 51 +++++++++++++ day-23/internal/one/solve_internal_test.go | 15 ++++ day-23/internal/one/util.go | 22 ++++++ day-23/internal/set/set.go | 75 +++++++++++++++++++ day-23/internal/two/benchmark | 6 ++ day-23/internal/two/clique.go | 16 +++++ day-23/internal/two/solve.go | 83 ++++++++++++++++++++++ day-23/internal/two/solve_internal_test.go | 15 ++++ day-23/internal/two/util.go | 22 ++++++ day-23/makefile | 30 ++++++++ day-23/solve.go | 20 ++++++ day-23/solve_internal_test.go | 15 ++++ 17 files changed, 454 insertions(+) create mode 100644 day-23/benchmark create mode 100644 day-23/cmd/cli/main.go create mode 100644 day-23/go.mod create mode 100644 day-23/go.sum create mode 100644 day-23/internal/one/benchmark create mode 100644 day-23/internal/one/solve.go create mode 100644 day-23/internal/one/solve_internal_test.go create mode 100644 day-23/internal/one/util.go create mode 100644 day-23/internal/set/set.go create mode 100644 day-23/internal/two/benchmark create mode 100644 day-23/internal/two/clique.go create mode 100644 day-23/internal/two/solve.go create mode 100644 day-23/internal/two/solve_internal_test.go create mode 100644 day-23/internal/two/util.go create mode 100644 day-23/makefile create mode 100644 day-23/solve.go create mode 100644 day-23/solve_internal_test.go diff --git a/day-23/benchmark b/day-23/benchmark new file mode 100644 index 0000000..a766f3d --- /dev/null +++ b/day-23/benchmark @@ -0,0 +1,15 @@ +goos: linux +goarch: amd64 +pkg: github.com/onyx-and-iris/aoc2024/day-23 +cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz +BenchmarkSolve-12 1000000000 0.2466 ns/op +BenchmarkSolve-12 1000000000 0.2520 ns/op +BenchmarkSolve-12 1000000000 0.2490 ns/op +BenchmarkSolve-12 1000000000 0.2484 ns/op +BenchmarkSolve-12 1000000000 0.2484 ns/op +BenchmarkSolve-12 1000000000 0.2639 ns/op +BenchmarkSolve-12 1000000000 0.2497 ns/op +BenchmarkSolve-12 1000000000 0.2424 ns/op +BenchmarkSolve-12 1000000000 0.2506 ns/op +BenchmarkSolve-12 1000000000 0.2464 ns/op +ok github.com/onyx-and-iris/aoc2024/day-23 37.530s diff --git a/day-23/cmd/cli/main.go b/day-23/cmd/cli/main.go new file mode 100644 index 0000000..d85032c --- /dev/null +++ b/day-23/cmd/cli/main.go @@ -0,0 +1,41 @@ +/******************************************************************************** + Advent of Code 2024 - day-23 +********************************************************************************/ + +package main + +import ( + "embed" + "flag" + "fmt" + "slices" + + log "github.com/sirupsen/logrus" + + problems "github.com/onyx-and-iris/aoc2024/day-23" +) + +//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: %s\n", one, two) +} diff --git a/day-23/go.mod b/day-23/go.mod new file mode 100644 index 0000000..d5e23dd --- /dev/null +++ b/day-23/go.mod @@ -0,0 +1,7 @@ +module github.com/onyx-and-iris/aoc2024/day-23 + +go 1.23.3 + +require github.com/sirupsen/logrus v1.9.3 + +require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect diff --git a/day-23/go.sum b/day-23/go.sum new file mode 100644 index 0000000..21f9bfb --- /dev/null +++ b/day-23/go.sum @@ -0,0 +1,15 @@ +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 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/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-23/internal/one/benchmark b/day-23/internal/one/benchmark new file mode 100644 index 0000000..071c3cb --- /dev/null +++ b/day-23/internal/one/benchmark @@ -0,0 +1,6 @@ +goos: linux +goarch: amd64 +pkg: github.com/onyx-and-iris/aoc2024/day-23/internal/one +cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz +BenchmarkSolve-12 1000000000 0.003422 ns/op +ok github.com/onyx-and-iris/aoc2024/day-23/internal/one 0.029s diff --git a/day-23/internal/one/solve.go b/day-23/internal/one/solve.go new file mode 100644 index 0000000..1fdb0c4 --- /dev/null +++ b/day-23/internal/one/solve.go @@ -0,0 +1,51 @@ +package one + +import ( + "bytes" + "slices" + "strings" + + "github.com/onyx-and-iris/aoc2024/day-23/internal/set" +) + +func Solve(buf []byte) (int, error) { + r := bytes.NewReader(buf) + connections, err := parseLines(r) + if err != nil { + return 0, err + } + + networks := make(map[string]*set.Set) + for _, connection := range connections { + if _, found := networks[connection[0]]; !found { + networks[connection[0]] = set.New() + } + networks[connection[0]].Add(connection[1]) + + if _, found := networks[connection[1]]; !found { + networks[connection[1]] = set.New() + } + networks[connection[1]].Add(connection[0]) + } + + setIdentifiers := make(map[string]struct{}) + for name := range networks { + if !strings.HasPrefix(name, "t") { + continue + } + + for _, node := range networks[name].List() { + for _, node2 := range networks[node].List() { + if slices.Contains(networks[node2].List(), name) { + s := set.New() + s.Add(name) + s.Add(node) + s.Add(node2) + setIdentifiers[s.String()] = struct{}{} + } + } + } + } + + return len(setIdentifiers), nil +} diff --git a/day-23/internal/one/solve_internal_test.go b/day-23/internal/one/solve_internal_test.go new file mode 100644 index 0000000..e5fd7ea --- /dev/null +++ b/day-23/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-23/internal/one/util.go b/day-23/internal/one/util.go new file mode 100644 index 0000000..af1f493 --- /dev/null +++ b/day-23/internal/one/util.go @@ -0,0 +1,22 @@ +package one + +import ( + "bufio" + "io" + "strings" +) + +func parseLines(r io.Reader) ([][]string, error) { + connections := [][]string{} + + scanner := bufio.NewScanner(r) + for scanner.Scan() { + connections = append(connections, strings.Split(scanner.Text(), "-")) + } + + if err := scanner.Err(); err != nil { + return nil, err + } + + return connections, nil +} diff --git a/day-23/internal/set/set.go b/day-23/internal/set/set.go new file mode 100644 index 0000000..11d1ca4 --- /dev/null +++ b/day-23/internal/set/set.go @@ -0,0 +1,75 @@ +package set + +import ( + "fmt" + "slices" +) + +// Set is a collection of unique elements +type Set struct { + elements map[string]struct{} +} + +// New creates a new set +func New() *Set { + return &Set{ + elements: make(map[string]struct{}), + } +} + +func (s *Set) String() string { + l := s.List() + slices.Sort(l) + return fmt.Sprintf("%v", l) +} + +// Add inserts an element into the set +func (s *Set) Add(value string) { + s.elements[value] = struct{}{} +} + +// Remove deletes an element from the set +func (s *Set) Remove(value string) { + delete(s.elements, value) +} + +// Contains checks if an element is in the set +func (s *Set) Contains(value string) bool { + _, found := s.elements[value] + return found +} + +// Size returns the number of elements in the set +func (s *Set) Size() int { + return len(s.elements) +} + +// List returns all elements in the set as a slice +func (s *Set) List() []string { + keys := make([]string, 0, len(s.elements)) + for key := range s.elements { + keys = append(keys, key) + } + return keys +} + +func (s *Set) Union(other *Set) *Set { + result := New() + for key := range s.elements { + result.Add(key) + } + for key := range other.elements { + result.Add(key) + } + return result +} + +func (s *Set) Intersection(other *Set) *Set { + result := New() + for key := range s.elements { + if other.Contains(key) { + result.Add(key) + } + } + return result +} diff --git a/day-23/internal/two/benchmark b/day-23/internal/two/benchmark new file mode 100644 index 0000000..dede114 --- /dev/null +++ b/day-23/internal/two/benchmark @@ -0,0 +1,6 @@ +goos: linux +goarch: amd64 +pkg: github.com/onyx-and-iris/aoc2024/day-23/internal/two +cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz +BenchmarkSolve-12 1000000000 0.2431 ns/op +ok github.com/onyx-and-iris/aoc2024/day-23/internal/two 3.693s diff --git a/day-23/internal/two/clique.go b/day-23/internal/two/clique.go new file mode 100644 index 0000000..34893e2 --- /dev/null +++ b/day-23/internal/two/clique.go @@ -0,0 +1,16 @@ +package two + +import "slices" + +type clique struct { + values []string +} + +func (c *clique) Len() int { + return len(c.values) +} + +func (c *clique) sorted() []string { + slices.Sort(c.values) + return c.values +} diff --git a/day-23/internal/two/solve.go b/day-23/internal/two/solve.go new file mode 100644 index 0000000..89b543a --- /dev/null +++ b/day-23/internal/two/solve.go @@ -0,0 +1,83 @@ +package two + +import ( + "bytes" + "slices" + "strings" + "sync" + + "github.com/onyx-and-iris/aoc2024/day-23/internal/set" + log "github.com/sirupsen/logrus" +) + +func Solve(buf []byte) (string, error) { + r := bytes.NewReader(buf) + connections, err := parseLines(r) + if err != nil { + return "", err + } + + networks := make(map[string]*set.Set) + nodes := set.New() + for _, connection := range connections { + if _, found := networks[connection[0]]; !found { + networks[connection[0]] = set.New() + } + networks[connection[0]].Add(connection[1]) + + if _, found := networks[connection[1]]; !found { + networks[connection[1]] = set.New() + } + networks[connection[1]].Add(connection[0]) + + nodes.Add(connection[0]) + nodes.Add(connection[1]) + } + + cliquesChan := make(chan clique) + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + next([]string{}, nodes, set.New(), cliquesChan, networks, &wg) + }() + + go func() { + wg.Wait() + close(cliquesChan) + }() + + var maxLen int + var maxLenClique []string + for c := range cliquesChan { + if c.Len() > maxLen { + maxLen = c.Len() + maxLenClique = c.sorted() + log.Debug(c) + } + } + + return strings.Join(maxLenClique, ","), nil +} + +func next( + R []string, + P, X *set.Set, + cliquesChan chan<- clique, + networks map[string]*set.Set, + wg *sync.WaitGroup, +) { + if P.Size() == 0 && X.Size() == 0 { + cliquesChan <- clique{slices.Clone(R)} + return + } + + for _, v := range P.List() { + nextR := append(R, v) + + next(nextR, P.Intersection(networks[v]), X.Intersection(networks[v]), cliquesChan, networks, wg) + + P.Remove(v) + X.Add(v) + } +} diff --git a/day-23/internal/two/solve_internal_test.go b/day-23/internal/two/solve_internal_test.go new file mode 100644 index 0000000..ecb05d5 --- /dev/null +++ b/day-23/internal/two/solve_internal_test.go @@ -0,0 +1,15 @@ +package two + +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-23/internal/two/util.go b/day-23/internal/two/util.go new file mode 100644 index 0000000..aad2ada --- /dev/null +++ b/day-23/internal/two/util.go @@ -0,0 +1,22 @@ +package two + +import ( + "bufio" + "io" + "strings" +) + +func parseLines(r io.Reader) ([][]string, error) { + connections := [][]string{} + + scanner := bufio.NewScanner(r) + for scanner.Scan() { + connections = append(connections, strings.Split(scanner.Text(), "-")) + } + + if err := scanner.Err(); err != nil { + return nil, err + } + + return connections, nil +} diff --git a/day-23/makefile b/day-23/makefile new file mode 100644 index 0000000..dfe0c43 --- /dev/null +++ b/day-23/makefile @@ -0,0 +1,30 @@ +program = day-23 + +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-23/solve.go b/day-23/solve.go new file mode 100644 index 0000000..a88c1ba --- /dev/null +++ b/day-23/solve.go @@ -0,0 +1,20 @@ +package daytwentythree + +import ( + "github.com/onyx-and-iris/aoc2024/day-23/internal/one" + "github.com/onyx-and-iris/aoc2024/day-23/internal/two" +) + +func Solve(buf []byte) (int, string, error) { + answerOne, err := one.Solve(buf) + if err != nil { + return 0, "", err + } + + answerTwo, err := two.Solve(buf) + if err != nil { + return 0, "", err + } + + return answerOne, answerTwo, nil +} diff --git a/day-23/solve_internal_test.go b/day-23/solve_internal_test.go new file mode 100644 index 0000000..07741a9 --- /dev/null +++ b/day-23/solve_internal_test.go @@ -0,0 +1,15 @@ +package daytwentythree + +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) +}