add day-23 + benchmarks

This commit is contained in:
2024-12-23 19:56:13 +00:00
parent b2e0108fb0
commit ef120d05d4
17 changed files with 454 additions and 0 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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
}