Skip to content

Commit 72a290e

Browse files
committed
solve day 23
1 parent 1a6abc2 commit 72a290e

File tree

5 files changed

+3652
-1
lines changed

5 files changed

+3652
-1
lines changed

\

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"sort"
7+
"strings"
8+
9+
"github.com/ayo-awe/advent-of-code-2024/aoc"
10+
)
11+
12+
func main() {
13+
input, err := aoc.ReadInputLineByLine("input.txt")
14+
if err != nil {
15+
log.Fatal(err)
16+
}
17+
18+
network := buildNetwork(input)
19+
20+
fmt.Println("solution to part one: ", PartOne(network))
21+
fmt.Println("solution to part two: ", PartTwo())
22+
}
23+
24+
func PartOne(network map[string]map[string]struct{}) int {
25+
matches := make(map[string]struct{})
26+
for node, subnet := range network {
27+
if !strings.HasPrefix(node, "t") {
28+
continue
29+
}
30+
31+
var subnodes []string
32+
for subnode := range subnet {
33+
subnodes = append(subnodes, subnode)
34+
}
35+
36+
for i := 0; i < len(subnodes)-1; i++ {
37+
for j := i + 1; j < len(subnodes); j++ {
38+
subA, subB := subnodes[i], subnodes[j]
39+
lookupKey := []string{node, subA, subB}
40+
sort.Strings(lookupKey)
41+
42+
key := strings.Join(lookupKey, ",")
43+
44+
if _, exists := matches[key]; exists {
45+
continue
46+
}
47+
48+
if _, connected := network[subA][subB]; connected {
49+
matches[key] = struct{}{}
50+
}
51+
}
52+
}
53+
}
54+
55+
return len(matches)
56+
}
57+
58+
func PartTwo(network map[string]map[string]struct{}) string {
59+
matches := make(map[string]struct{})
60+
for node, subnet := range network {
61+
if !strings.HasPrefix(node, "t") {
62+
continue
63+
}
64+
65+
var subnodes []string
66+
for subnode := range subnet {
67+
subnodes = append(subnodes, subnode)
68+
}
69+
70+
for i := 0; i < len(subnodes)-1; i++ {
71+
for j := i + 1; j < len(subnodes); j++ {
72+
subA, subB := subnodes[i], subnodes[j]
73+
lookupKey := []string{node, subA, subB}
74+
sort.Strings(lookupKey)
75+
76+
key := strings.Join(lookupKey, ",")
77+
78+
if _, exists := matches[key]; exists {
79+
continue
80+
}
81+
82+
if _, connected := network[subA][subB]; connected {
83+
matches[key] = struct{}{}
84+
}
85+
}
86+
}
87+
}
88+
89+
return 0
90+
}
91+
92+
func buildNetwork(connections []string) map[string]map[string]struct{} {
93+
network := make(map[string]map[string]struct{})
94+
for _, connection := range connections {
95+
nodes := strings.Split(connection, "-")
96+
node1, node2 := nodes[0], nodes[1]
97+
98+
if _, ok := network[node1]; !ok {
99+
network[node1] = make(map[string]struct{})
100+
}
101+
102+
if _, ok := network[node2]; !ok {
103+
network[node2] = make(map[string]struct{})
104+
}
105+
106+
network[node1][node2] = struct{}{}
107+
network[node2][node1] = struct{}{}
108+
}
109+
return network
110+
}

day_22/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,3 @@ func nthSecret(secret, n int) []int {
103103
}
104104
return secrets
105105
}
106-

day_23/ex.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
kh-tc
2+
qp-kh
3+
de-cg
4+
ka-co
5+
yn-aq
6+
qp-ub
7+
cg-tb
8+
vc-aq
9+
tb-ka
10+
wh-tc
11+
yn-cg
12+
kh-ub
13+
ta-co
14+
de-co
15+
tc-td
16+
tb-wq
17+
wh-td
18+
ta-ka
19+
td-qp
20+
aq-cg
21+
wq-ub
22+
ub-vc
23+
de-ta
24+
wq-aq
25+
wq-vc
26+
wh-yn
27+
ka-de
28+
kh-ta
29+
co-tc
30+
wh-qp
31+
tb-vc
32+
td-yn

0 commit comments

Comments
 (0)