Advent-of-Code/2020/Day 6/Solution.py

46 lines
1.4 KiB
Python

input_file = "input.txt"
answers = []
participants = []
with open(input_file) as group_answers:
line = group_answers.readline()
answers.append({})
# Count from zero as the last line return is for the group separation
participant_count = 0
while line:
if line == "\n":
answers.append({})
participants.append(participant_count)
participant_count = 0
line = group_answers.readline()
continue
for character in line:
if character == '\n':
participant_count += 1
break
# Count answers for each questions
if character in answers[-1]:
answers[-1][character] += 1
else:
answers[-1][character] = 1
line = group_answers.readline()
print(f"The total number of positive answers is {sum([len(answer_dict) for answer_dict in answers])}")
sum_of_all_yes = 0
# Compare the count of yes for each answer with the count of people in the group.
# Do so by iterating over both arrays at once and then iterating over the keys of the answer dictionary.
for index in range(len(participants)):
for key in answers[index]:
if answers[index][key] == participants[index]:
sum_of_all_yes += 1
print(f"The total number of positive answer by each member of each group is {sum_of_all_yes}")