Skip to content

Commit

Permalink
Solution for 2024, day 23
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianoq committed Dec 23, 2024
1 parent f3de136 commit 66faf15
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 2024/23/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
input:
http "https://adventofcode.com/2024/day/23/input" "Cookie:session=${AOC_SESSION};" >input

main1:
go build -o main1 main1.go common.go

main2:
go build -o main2 main2.go common.go

.PHONY: run1 run2 clean

run1: main1 input
./main1 <input

run2: main2 input
./main2 <input

clean:
rm -f main1 main2 input

93 changes: 93 additions & 0 deletions 2024/23/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"bufio"
"os"
"sort"
"strings"
)

type Set map[string]struct{}

func (s *Set) ToKey(others ...string) string {
list := make([]string, 0, len(*s)+len(others))
for k := range *s {
list = append(list, k)
}
for _, other := range others {
list = append(list, other)
}
sort.Strings(list)
return strings.Join(list, ",")
}

func (s *Set) FromKey(key string) {
m := Set{}
for _, s := range strings.Split(key, ",") {
m[s] = struct{}{}
}
*s = m
}

type Graph map[string]Set

func (g Graph) AddEdge(from, to string) {
if _, ok := g[from]; !ok {
g[from] = Set{}
}
g[from][to] = struct{}{}
}

func (g Graph) Connected(from, to string) bool {
if f, ok := g[from]; ok {
_, ok := f[to]
return ok
}
return false
}

func (g Graph) Keys() Set {
keys := Set{}
for n := range g {
keys[n] = struct{}{}
}
return keys
}

func parseInput() Graph {
graph := Graph{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
ff := strings.Split(scanner.Text(), "-")
graph.AddEdge(ff[0], ff[1])
graph.AddEdge(ff[1], ff[0])
}
return graph
}

func (g Graph) Expand(clusters Set) Set {
res := Set{}
for clusterStr := range clusters {
cluster := Set{}
cluster.FromKey(clusterStr)
for node := range g[clusterStr[:2]] {
if _, ok := cluster[node]; ok {
continue
}
if !g.connectedToAll(node, cluster) {
continue
}
res[cluster.ToKey(node)] = struct{}{}
}
}
return res
}

func (g Graph) connectedToAll(node string, cluster Set) bool {
for old := range cluster {
if !g.Connected(old, node) {
return false
}
}
return true
}
22 changes: 22 additions & 0 deletions 2024/23/main1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"strings"
)

func main() {
g := parseInput()

res := g.Keys() // single nodes
res = g.Expand(res) // pairs
res = g.Expand(res) // triplets

count := 0
for c := range res {
if c[0] == 't' || strings.Contains(c, ",t") {
count++
}
}
fmt.Println(count)
}
17 changes: 17 additions & 0 deletions 2024/23/main2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import "fmt"

func main() {
g := parseInput()

var lastNotEmpty Set
for res := g.Keys(); len(res) > 0; res = g.Expand(res) {
lastNotEmpty = res
}

for k := range lastNotEmpty {
fmt.Println(k)
return
}
}

0 comments on commit 66faf15

Please sign in to comment.