You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

32 lines
911 B

# count answers in that group
def count_answers(line):
my_list = list(line)
my_set = set(my_list)
length = len(my_set)
return length
# count which answers were answered by all
def count_answers_all(group):
answers = group.split('\n')
z = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z'}
intersect = z.intersection(* [set(answer) for answer in answers if answer != ''])
return len(intersect)
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input06") as f:
lines = f.read()
# count answered letters
lines1 = lines.replace("\n\n", '\t').replace("\n", "").split("\t")
count = sum(count_answers(l) for l in lines1)
print(count)
# count answered by all
groups = lines.split("\n\n")
count2 = sum(count_answers_all(g) for g in groups)
print(count2)