76 lines
2.6 KiB
Python
76 lines
2.6 KiB
Python
|
"""
|
||
|
--- Part Two ---
|
||
|
|
||
|
As you finish identifying the misplaced items, the Elves come to you
|
||
|
with another issue.
|
||
|
|
||
|
For safety, the Elves are divided into groups of three. Every Elf
|
||
|
carries a badge that identifies their group. For efficiency, within
|
||
|
each group of three Elves, the badge is the only item type carried by
|
||
|
all three Elves. That is, if a group's badge is item type B, then all
|
||
|
three Elves will have item type B somewhere in their rucksack, and at
|
||
|
most two of the Elves will be carrying any other item type.
|
||
|
|
||
|
The problem is that someone forgot to put this year's updated
|
||
|
authenticity sticker on the badges. All of the badges need to be pulled
|
||
|
out of the rucksacks so the new authenticity stickers can be attached.
|
||
|
|
||
|
Additionally, nobody wrote down which item type corresponds to each
|
||
|
group's badges. The only way to tell which item type is the right one
|
||
|
is by finding the one item type that is common between all three Elves
|
||
|
in each group.
|
||
|
|
||
|
Every set of three lines in your list corresponds to a single group,
|
||
|
but each group can have a different badge item type. So, in the above
|
||
|
example, the first group's rucksacks are the first three lines:
|
||
|
|
||
|
vJrwpWtwJgWrhcsFMMfFFhFp
|
||
|
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
|
||
|
PmmdzqPrVvPwwTWBwg
|
||
|
|
||
|
And the second group's rucksacks are the next three lines:
|
||
|
|
||
|
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
|
||
|
ttgJtRGJQctTZtZT
|
||
|
CrZsJsPPZsGzwwsLwLmpwMDw
|
||
|
|
||
|
In the first group, the only item type that appears in all three
|
||
|
rucksacks is lowercase r; this must be their badges. In the second
|
||
|
group, their badge item type must be Z.
|
||
|
|
||
|
Priorities for these items must still be found to organize the sticker
|
||
|
attachment efforts: here, they are 18 (r) for the first group and 52
|
||
|
(Z) for the second group. The sum of these is 70.
|
||
|
|
||
|
Find the item type that corresponds to the badges of each three-Elf
|
||
|
group. What is the sum of the priorities of those item types?
|
||
|
"""
|
||
|
import string
|
||
|
|
||
|
|
||
|
def main():
|
||
|
total_priority = 0
|
||
|
priority_index = f"{string.ascii_lowercase}{string.ascii_uppercase}"
|
||
|
|
||
|
with open("input.txt", "r", encoding="utf-8") as f:
|
||
|
group = []
|
||
|
|
||
|
for line in f:
|
||
|
line_contents = line.strip()
|
||
|
if not line_contents:
|
||
|
continue
|
||
|
|
||
|
group.append(line_contents)
|
||
|
|
||
|
if len(group) == 3:
|
||
|
common_item = set(group[0]).intersection(set(group[1])).intersection(set(group[2])).pop()
|
||
|
common_item_priority = priority_index.index(common_item) + 1
|
||
|
total_priority += common_item_priority
|
||
|
group = []
|
||
|
|
||
|
print(f"Total group priority is {total_priority}")
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|