day 12
digital plumber
approach
each line is parsed into a program id and the ids it talks to, and every pair is unioned in a small union-find (UFS) with path compression.
part 1 counts the ids whose root matches the root of 0. part 2 tallies the ids by root and returns how many distinct roots there are, which is the number of groups.
the trick
connected components come straight out of union-find: one union per pipe, then count roots.
python
def _root(self, a):
if self.parents[a] == a:
return a
ra = self._root(self.parents[a])
self.parents[a] = ra
return ra