Advent of Code: Day 12

For Day 12, the challenge is to count the number of isolated groups within a graph. My Python solution is below:

connections = {}

with open("input.txt", "r") as o:
    for line in"\n"):
        p = line.split(" <-> ")
        connections[int(p[0])] = [int(i.strip()) for i in p[1].split(",")]

groups = {}
marked = []

def check(ptr, group=None):
    if ptr in marked:

    if group is None:
        group = ptr
        groups[group] = []


    for c in connections[ptr]:
        check(c, group)

for i in connections.keys():

print "Part 1:", len(groups[0])
print "Part 2:", len(groups)

I found this one to be significantly easier than recent days.

Advent of Code runs every day up to Christmas, you should join in!.

Show Comments

Get the latest posts delivered right to your inbox.