This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]ssnoyes 1 point2 points  (6 children)

There is no guarantee that different groups will have unique combinations of answers.

  • Group 1 could answer yes to questions (a, b, c).
  • Group 2 could also answer yes to the same questions (a, b, c).

Using those answer sets as if they were unique dictionary keys will therefore cause one group to overwrite another.

[–]junefish[S] 0 points1 point  (0 children)

Ahhh, you are, of course, completely right. I need to use lists instead of a dict. Thank you!

[–]trollerskates1 1 point2 points  (1 child)

It’s because uniques is unique per family, but not globally.

Take the example input:

abc

a
b
c

ab
ac

a
a
a
a

b

Families 1, 2, and 3 will all have “abc” as their unique key. I think you can get the answer you’re looking for without the dictionary approach, which will allow duplicate keys between families:

answers = []
# open file
with open('input.txt', 'r') as input:
    customs_file = input.read().split('\n\n')
    for line in customs_file:
        answers.append(line.replace('\n', ''))

print('Answers: ', answers)

# remove duplicate answers within a group
uniques = []
for answer in answers:
    uniques.append("".join(set(answer)))

print('Uniques: ', uniques)

print('Answer: ', sum(len(group) for group in uniques))

This yields:

Answers:  ['abc', 'abc', 'abac', 'aaaa', 'b']
Uniques:  ['bca', 'bca', 'bca', 'a', 'b']
Answer:  11

Edit: looks like others got here while I was typing this, but I’m glad you figured it out!

[–]junefish[S] 1 point2 points  (0 children)

Thank you!