Sean McLemon | Advent of Code

Home | Czech | Blog | GitHub | Advent Of Code | Notes


2020-12-06 - Custom Customs

(original .ipynb)

Day 6 puzzle input is a list of positive responses to customs declaration questions for groups of travellers, with each group separated by a blank line (mine is here). Part 1 involves counting the number of questions that anyone in the group answered "yes" to, collecting the sum of these for each group and summing them. Part 2 is similar but for where all members of a group answered in the affirmative.

test_input_str = """abc

a
b
c

ab
ac

a
a
a
a

b"""

test_input_lines = test_input_str.split("\n")
puzzle_input_lines = open("puzzle_input/day6.txt").read().split("\n")

def find_group_union_sum(input_lines):
    current_group = set()
    group_totals = []
    for line in input_lines:
        if len(line.strip()) == 0:
                group_totals.append(len(current_group))
                current_group = set()
        else:
            current_person = set(a for a in line)
            current_group = current_group.union(current_person)

    if len(current_group) > 0:
        group_totals.append(len(current_group))
    
    return sum(group_totals)
    
assert 11 == find_group_union_sum(test_input_lines)
print(find_group_union_sum(puzzle_input_lines))
6443
def find_group_intersection_sum(input_lines):
    current_group = None
    group_totals = []
    for line in input_lines:

        if len(line.strip()) == 0:
                group_totals.append(len(current_group))
                current_group = None

        else:
            current_person = set(a for a in line)

            if current_group == None:
                current_group = current_person
            else:
                current_group = current_group.intersection(current_person)

    if current_group != None:
        group_totals.append(len(current_group))
        
    return sum(group_totals)
        
assert 6 == find_group_intersection_sum(test_input_lines)
print(find_group_intersection_sum(puzzle_input_lines))
3232