mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-09 02:23:36 +00:00
add day-23 + benchmarks
This commit is contained in:
6
day-23/internal/two/benchmark
Normal file
6
day-23/internal/two/benchmark
Normal file
@@ -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
|
||||
16
day-23/internal/two/clique.go
Normal file
16
day-23/internal/two/clique.go
Normal file
@@ -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
|
||||
}
|
||||
83
day-23/internal/two/solve.go
Normal file
83
day-23/internal/two/solve.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
15
day-23/internal/two/solve_internal_test.go
Normal file
15
day-23/internal/two/solve_internal_test.go
Normal file
@@ -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)
|
||||
}
|
||||
22
day-23/internal/two/util.go
Normal file
22
day-23/internal/two/util.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user