Mary Knize

Advent of Code 2020, Day 6 -- Custom Customs

3 min read

Advent Of Code

My Advent of Code solutions, done in Python. Refactors and solutions in other languages will be added if/when they're done.

Full code for Day 6 can be found on Github.

You can participate in Advent of Code at

The Day 6 problem can be found at


Solution, part 1

This problem was actually really easy. I decided what I needed to do was get each group's info onto one line, then simply put each line's data into a set. This will de-duplicate each letter and I can count the number of letters in each set by getting the length of the set, then return the sum of those counts.

You'll notice that I changed up how I parse the puzzle input this time. I'd come up with that while trying to refactor Day 4 (which still hasn't been successful). I'm replacing newlines (\n\n) with "---" as a placeholder, then removing single returns (\n). This gets each group's data onto one line. Then, I split the text by "---", which creates a list I can iterate over.

I'm also opening, reading, and parsing the puzzle input separately for each part of this problem, because part 2 is going to be slightly different.

# Put each line into a set, get the length of the set,
# and sum the counts in the list.
def individual_yes_answers():
    puzzle_input = open("input.txt", "r")
    txt ="\n\n", "---").replace("\n", '').strip()
    data = txt.split("---")
    dups_removed = [set(line) for line in data]
    counts = [len(arr) for arr in dups_removed]
    return sum(counts)


Solution, part 2

For part 2, I'm parsing the puzzle input similarly, but replacing newlines at the end of strings (\n) with a space instead of removing them entirely. That way, I can get the group's answers but also count the number of people in the group.

What I'm doing here is creating a dictionary for each group's answers, and as I iterate over each letter, increment the number in the dictionary. Then, I check each dictionary entry to see if the number is equal to the length of the list of people in the group. If so, that means everyone answered that question, so I increment valid_answers by one.

# Creating a dictionary of letters for each line.
# If any of the letters in the dictionary are equal to
# the length of the list created by splitting the line by
# spaces, it's considered a valid answer.
# Increment the number of valid answers by 1, and return that value.
def total_yes_answers():
    puzzle_input = open("input.txt", "r")
    txt ="\n\n", "---").replace("\n", ' ').strip()
    data = txt.split("---")
    valid_answers = 0
    for line in data:
        dct = {}
        arr = line.split(" ")
        for answers in arr:
            letters = list(answers)
            for l in letters:
                if l in dct:
                    dct[l] += 1
                    dct[l] = 1
        for key in dct:
            if dct[key] == len(arr):
                valid_answers += 1
    return valid_answers


More Advent of Code

Canonical URL