Day 6 of 2020 done in Python

This commit is contained in:
Teo-CD 2020-12-17 17:39:24 +01:00
parent e3cb2ddd48
commit 1b76468550
2 changed files with 2090 additions and 0 deletions

46
2020/Day 6/Solution.py Normal file
View file

@ -0,0 +1,46 @@
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}")

2044
2020/Day 6/input.txt Normal file

File diff suppressed because it is too large Load diff