Skip to content

Commit 651ab98

Browse files
committed
feat: Solving day 23
1 parent 566ce90 commit 651ab98

File tree

3 files changed

+3583
-0
lines changed

3 files changed

+3583
-0
lines changed

docs/day23.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
url: "https://adventofcode.com/2024/day/23"
3+
---
4+
5+
# Day 23: LAN Party
6+
7+
As The Historians wander around a secure area at Easter Bunny HQ, you come across posters for a LAN party scheduled for today! Maybe you can find it; you connect to a nearby datalink port and download a map of the local network (your puzzle input).
8+
9+
The network map provides a list of every connection between two computers. For example:
10+
11+
```txt
12+
kh-tc
13+
qp-kh
14+
de-cg
15+
ka-co
16+
yn-aq
17+
qp-ub
18+
cg-tb
19+
vc-aq
20+
tb-ka
21+
wh-tc
22+
yn-cg
23+
kh-ub
24+
ta-co
25+
de-co
26+
tc-td
27+
tb-wq
28+
wh-td
29+
ta-ka
30+
td-qp
31+
aq-cg
32+
wq-ub
33+
ub-vc
34+
de-ta
35+
wq-aq
36+
wq-vc
37+
wh-yn
38+
ka-de
39+
kh-ta
40+
co-tc
41+
wh-qp
42+
tb-vc
43+
td-yn
44+
```
45+
46+
Each line of text in the network map represents a single connection; the line `kh-tc` represents a connection between the computer named `kh` and the computer named `tc`. Connections aren't directional; `tc-kh` would mean exactly the same thing.
47+
48+
LAN parties typically involve multiplayer games, so maybe you can locate it by finding groups of connected computers. Start by looking for sets of three computers where each computer in the set is connected to the other two computers.
49+
50+
In this example, there are `12` such sets of three inter-connected computers:
51+
52+
```txt
53+
aq,cg,yn
54+
aq,vc,wq
55+
co,de,ka
56+
co,de,ta
57+
co,ka,ta
58+
de,ka,ta
59+
kh,qp,ub
60+
qp,td,wh
61+
tb,vc,wq
62+
tc,td,wh
63+
td,wh,yn
64+
ub,vc,wq
65+
```
66+
67+
If the Chief Historian is here, and he's at the LAN party, it would be best to know that right away. You're pretty sure his computer's name starts with t, so consider only sets of three computers where at least one computer's name starts with `t`. That narrows the list down to `7` sets of three inter-connected computers:
68+
69+
```txt
70+
co,de,ta
71+
co,ka,ta
72+
de,ka,ta
73+
qp,td,wh
74+
tb,vc,wq
75+
tc,td,wh
76+
td,wh,yn
77+
```
78+
79+
Find all the sets of three inter-connected computers. How many contain at least one computer with a name that starts with `t`?

src/day23/day23.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package day23
2+
3+
import (
4+
"strings"
5+
)
6+
7+
const FILTER_PREFIX = 't'
8+
9+
type connection struct {
10+
left, right string
11+
}
12+
13+
func Solve(input string) uint {
14+
dict := make(map[string][]string)
15+
isConnected := make(map[connection]bool)
16+
connections := parseInput(input)
17+
for _, conn := range connections {
18+
if conn.left[0] == FILTER_PREFIX {
19+
dict[conn.left] = append(dict[conn.left], conn.right)
20+
}
21+
if conn.right[0] == FILTER_PREFIX {
22+
dict[conn.right] = append(dict[conn.right], conn.left)
23+
}
24+
isConnected[conn] = true
25+
isConnected[connection{left: conn.right, right: conn.left}] = true
26+
}
27+
28+
var result, doubles, triples uint
29+
for _, v := range dict {
30+
if len(v) < 2 {
31+
continue
32+
}
33+
34+
for i := 0; i < len(v)-1; i++ {
35+
for j := i + 1; j < len(v); j++ {
36+
x := isConnected[connection{left: v[i], right: v[j]}]
37+
if x {
38+
result++
39+
if v[i][0] == FILTER_PREFIX || v[j][0] == FILTER_PREFIX {
40+
if v[i][0] == v[j][0] {
41+
triples++
42+
} else {
43+
doubles++
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
return result - doubles/2 - 2*(triples/3)
51+
}
52+
53+
func parseInput(input string) []connection {
54+
result := make([]connection, 0)
55+
for _, line := range strings.Split(input, "\n") {
56+
splits := strings.Split(line, "-")
57+
if len(splits) != 2 {
58+
continue
59+
}
60+
61+
result = append(result, connection{left: splits[0], right: splits[1]})
62+
}
63+
return result
64+
}

0 commit comments

Comments
 (0)