Skip to content

Commit 66faf15

Browse files
committed
Solution for 2024, day 23
1 parent f3de136 commit 66faf15

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

2024/23/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
input:
2+
http "https://adventofcode.com/2024/day/23/input" "Cookie:session=${AOC_SESSION};" >input
3+
4+
main1:
5+
go build -o main1 main1.go common.go
6+
7+
main2:
8+
go build -o main2 main2.go common.go
9+
10+
.PHONY: run1 run2 clean
11+
12+
run1: main1 input
13+
./main1 <input
14+
15+
run2: main2 input
16+
./main2 <input
17+
18+
clean:
19+
rm -f main1 main2 input
20+

2024/23/common.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"os"
6+
"sort"
7+
"strings"
8+
)
9+
10+
type Set map[string]struct{}
11+
12+
func (s *Set) ToKey(others ...string) string {
13+
list := make([]string, 0, len(*s)+len(others))
14+
for k := range *s {
15+
list = append(list, k)
16+
}
17+
for _, other := range others {
18+
list = append(list, other)
19+
}
20+
sort.Strings(list)
21+
return strings.Join(list, ",")
22+
}
23+
24+
func (s *Set) FromKey(key string) {
25+
m := Set{}
26+
for _, s := range strings.Split(key, ",") {
27+
m[s] = struct{}{}
28+
}
29+
*s = m
30+
}
31+
32+
type Graph map[string]Set
33+
34+
func (g Graph) AddEdge(from, to string) {
35+
if _, ok := g[from]; !ok {
36+
g[from] = Set{}
37+
}
38+
g[from][to] = struct{}{}
39+
}
40+
41+
func (g Graph) Connected(from, to string) bool {
42+
if f, ok := g[from]; ok {
43+
_, ok := f[to]
44+
return ok
45+
}
46+
return false
47+
}
48+
49+
func (g Graph) Keys() Set {
50+
keys := Set{}
51+
for n := range g {
52+
keys[n] = struct{}{}
53+
}
54+
return keys
55+
}
56+
57+
func parseInput() Graph {
58+
graph := Graph{}
59+
scanner := bufio.NewScanner(os.Stdin)
60+
for scanner.Scan() {
61+
ff := strings.Split(scanner.Text(), "-")
62+
graph.AddEdge(ff[0], ff[1])
63+
graph.AddEdge(ff[1], ff[0])
64+
}
65+
return graph
66+
}
67+
68+
func (g Graph) Expand(clusters Set) Set {
69+
res := Set{}
70+
for clusterStr := range clusters {
71+
cluster := Set{}
72+
cluster.FromKey(clusterStr)
73+
for node := range g[clusterStr[:2]] {
74+
if _, ok := cluster[node]; ok {
75+
continue
76+
}
77+
if !g.connectedToAll(node, cluster) {
78+
continue
79+
}
80+
res[cluster.ToKey(node)] = struct{}{}
81+
}
82+
}
83+
return res
84+
}
85+
86+
func (g Graph) connectedToAll(node string, cluster Set) bool {
87+
for old := range cluster {
88+
if !g.Connected(old, node) {
89+
return false
90+
}
91+
}
92+
return true
93+
}

2024/23/main1.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func main() {
9+
g := parseInput()
10+
11+
res := g.Keys() // single nodes
12+
res = g.Expand(res) // pairs
13+
res = g.Expand(res) // triplets
14+
15+
count := 0
16+
for c := range res {
17+
if c[0] == 't' || strings.Contains(c, ",t") {
18+
count++
19+
}
20+
}
21+
fmt.Println(count)
22+
}

2024/23/main2.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
g := parseInput()
7+
8+
var lastNotEmpty Set
9+
for res := g.Keys(); len(res) > 0; res = g.Expand(res) {
10+
lastNotEmpty = res
11+
}
12+
13+
for k := range lastNotEmpty {
14+
fmt.Println(k)
15+
return
16+
}
17+
}

0 commit comments

Comments
 (0)