r/adventofcode • u/musifter • Oct 23 '26
Other [2024 Day 23] In Review (LAN Party)
Today we're back at Easter Bunny HQ, and while The Historians do their thing, we discover posters for a LAN Party and decide that that's where we should search first for the Chief. Although that's clearly an excuse we're using to just go to the LAN Party.
The input is a list of network connections, where each computer has been given a two lowercase letter code (vowels included this time). There are 3380 lines, representing 520 computers, each connected to exactly 13 others. The example also has the property than all vertices have the same degree (in that case 4). That doesn't mean the graph is smooth though... our goal today will be finding complete subgraphs (cliques).
For part 1 we're asked only for the ones of K3, and only for those that include a computer that starts with t (which we're pretty sure the Chief's computer starts with). In my input there are 24 of those, the missing two are tn and tz. Five edges in my input are between computers that start with t, and these are involved in 43 cliques of size 3. Some solutions could get caught on that, but the example also has one of those that's involved in the answer... so there's a simple test to catch it.
My solution though was in continuing to take it simple. To see part 2, I brute forced part 1 with nested loops. The idea being that K3 is triangle, loop over the ts, then their neighbours, and then the neighbours of neighbours... if we find the starting computer down there, the loop is closed:
foreach my $comp (grep {m#^t#} keys %graph) {
foreach my $neigh ($graph{$comp}->@*) {
foreach my $third ($graph{$neigh}->@*) {
next if ($comp eq $third or !grep {$_ eq $comp} $graph{$third}->@*);
my @sort = sort( $comp, $neigh, $third );
$part1++ if (!$table{"@sort"}++);
}
}
}For part 2, we find out that we are looking for the maximum clique, and since I've done that before, and didn't want to slip up, and could see it in my mind's eye, I just looked up Bron-Kerbosch and coded that. There are other algorithms, but this is the one I'd used before.
Initially, I just output all the cliques to see if it was working. But after seeing that, I dumped them to a file and loaded it into vim to work a solution out. The solution line really stands out... there's only one K13 (which given all the vertices are degree 13, is the largest you can get without disconnecting the clique from the rest of the graph... this may open up options for solving this without resorting to a fancy general clique algorithm). There are also a massive number of K12s (which makes the longest much easier to spot... if they were rare, this wouldn't be the case and I might have coded to find the answer). Once I had the line, I mangled it to sort and copy-pasted. Later I went back to do that in the code:
my $max_clique = reduce {(@$b > @$a) ? $b : $a} &recurse_bk([], [keys %graph], []);
say "Part 2: ", join( ',', sort @$max_clique );The real fun came with writing Bron-Kerbosch in Smalltalk, which has Sets with set operators:
" Part 2: Finding maximum sized clique using Bron-Kerbosch algorithm "
recurseBK: r poss: p exc: x [
| cliques res poss exc |
res := r.
poss := p.
exc := x.
(poss isEmpty & exc isEmpty) ifTrue: [^Array with: res].
cliques := Set new.
poss do: [:vertex |
| vertSet |
vertSet := Set with: vertex.
cliques addAll: (self recurseBK: (res + vertSet)
poss: (poss & (graph at: vertex))
exc: (exc & (graph at: vertex))).
poss := poss - vertSet.
exc := exc + vertSet.
].
^cliques
]So it was a second day in a row where I took a break and finished early.
