AdventOfCode

My solutions for Advent of Code 2020 in Python ⭐


Project maintained by benthecoder Hosted on GitHub Pages — Theme by mattgraham

Day 6: Custom Customs

Part 1

As your flight approaches the regional airport where you’ll switch to a much larger plane, customs declaration forms are distributed to the passengers.

The form asks a series of 26 yes-or-no questions marked a through z. All you need to do is identify the questions for which anyone in your group answers “yes”. Since your group is just you, this doesn’t take very long.

However, the person sitting next to you seems to be experiencing a language barrier and asks if you can help. For each of the people in their group, you write down the questions for which they answer “yes”, one per line. For example:

abcx abcy abcz

In this group, there are 6 questions to which anyone answered “yes”: a, b, c, x, y, and z. (Duplicate answers to the same question don’t count extra; each question counts at most once.)

Another group asks for your help, then another, and eventually you’ve collected answers from every group on the plane (your puzzle input). Each group’s answers are separated by a blank line, and within each group, each person’s answers are on a single line. For example:

` abc

a b c

ab ac

a a a a

b `

This list represents answers from five groups:

For each group, count the number of questions to which anyone answered “yes”. What is the sum of those counts?

Part 2

As you finish the last group’s customs declaration, you notice that you misread one word in the instructions:

You don’t need to identify the questions to which anyone answered “yes”; you need to identify the questions to which everyone answered “yes”!

Using the same example as above: ` abc

a b c

ab ac

a a a a

b ` This list represents answers from five groups:

For each group, count the number of questions to which everyone answered “yes”. What is the sum of those counts?


Solution

def read_file(file):
    with open(file) as f:
        file = f.read().replace('\n\n', '|').replace('\n', ' ').split('|')
    return file


def sol_1(answer):
    return len(set(answer.replace(' ', '')))


def sol_2(answer):
    return len(set.intersection(*map(set, answer.split())))


def main():
    part1, part2 = 0, 0
    answers = read_file("input.txt")

    for answer in answers:
        part1 += sol_1(answer)
        part2 += sol_2(answer)

    print(f"Part 1: {part1}\nPart 2: {part2}")


if __name__ == "__main__":
    main()

References