Part 1
In this puzzle, we have several lines describing connectiongs between programs. Each connected program can communicate bidirectionally. If two programs can communicate either directly or via one or more other programs, they belong in the same group. We should find the length of the group the program with id 0 is in.
We start by parsing each input line into a list of groups for the direct commmunication possibilities. To handle the indirect communication, we iterate over all pairs of groups and find if they have a program in common. If yes, the belong to the same group and are therefore combined into a single group. This is repeated until there is no connection between the groups anymore. Finally we find the group containing the id 0 and return its size.
Part 2
In part 2, we should find the number of distinct groups.
We can use the code from part 1 and count the number of groups at the end.
No comments:
Post a Comment