Skip to content

Solving day 23 #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions docs/day23.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
url: "https://adventofcode.com/2024/day/23"
---

# Day 23: LAN Party

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).

The network map provides a list of every connection between two computers. For example:

```txt
kh-tc
qp-kh
de-cg
ka-co
yn-aq
qp-ub
cg-tb
vc-aq
tb-ka
wh-tc
yn-cg
kh-ub
ta-co
de-co
tc-td
tb-wq
wh-td
ta-ka
td-qp
aq-cg
wq-ub
ub-vc
de-ta
wq-aq
wq-vc
wh-yn
ka-de
kh-ta
co-tc
wh-qp
tb-vc
td-yn
```

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.

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.

In this example, there are `12` such sets of three inter-connected computers:

```txt
aq,cg,yn
aq,vc,wq
co,de,ka
co,de,ta
co,ka,ta
de,ka,ta
kh,qp,ub
qp,td,wh
tb,vc,wq
tc,td,wh
td,wh,yn
ub,vc,wq
```

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:

```txt
co,de,ta
co,ka,ta
de,ka,ta
qp,td,wh
tb,vc,wq
tc,td,wh
td,wh,yn
```

Find all the sets of three inter-connected computers. How many contain at least one computer with a name that starts with `t`?
64 changes: 64 additions & 0 deletions src/day23/day23.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package day23

import (
"strings"
)

const FILTER_PREFIX = 't'

type connection struct {
left, right string
}

func Solve(input string) uint {
dict := make(map[string][]string)
isConnected := make(map[connection]bool)
connections := parseInput(input)
for _, conn := range connections {
if conn.left[0] == FILTER_PREFIX {
dict[conn.left] = append(dict[conn.left], conn.right)
}
if conn.right[0] == FILTER_PREFIX {
dict[conn.right] = append(dict[conn.right], conn.left)
}
isConnected[conn] = true
isConnected[connection{left: conn.right, right: conn.left}] = true
}

var result, doubles, triples uint
for _, v := range dict {
if len(v) < 2 {
continue
}

for i := 0; i < len(v)-1; i++ {
for j := i + 1; j < len(v); j++ {
x := isConnected[connection{left: v[i], right: v[j]}]
if x {
result++
if v[i][0] == FILTER_PREFIX || v[j][0] == FILTER_PREFIX {
if v[i][0] == v[j][0] {
triples++
} else {
doubles++
}
}
}
}
}
}
return result - doubles/2 - 2*(triples/3)
}

func parseInput(input string) []connection {
result := make([]connection, 0)
for _, line := range strings.Split(input, "\n") {
splits := strings.Split(line, "-")
if len(splits) != 2 {
continue
}

result = append(result, connection{left: splits[0], right: splits[1]})
}
return result
}
Loading
Loading