Day 3 (Puzzle 2) - Rucksack Reorganization

Successfully finished the second puzzle.
Took me a bit worrying what "and at most two of the Elves will be carrying any other item type" meant in the task text while my grouping loop was broken and skipping a rucksack every fourth rucksack while doing the group. Yikes.

Caught the error before trying a result though thanks to exceptions when things go wrong!
main
Peery 1 year ago
parent 34ee2b12a4
commit 47c6e0c941

@ -76,13 +76,33 @@ class Elf:
return was_win, round_score + RPS.RPS_SHAPE_TO_SCORE[own]
def check_rucksack_for_compartment_duplicates(self, rucksack: Tuple[List[str], List[str]]) -> str:
@staticmethod
def check_rucksacks_for_duplicates(rucksack1: Tuple[List[str], List[str]], rucksack2: Tuple[List[str], List[str]]) -> List[str]:
"""
Check a given rucksack for the duplicate item according to day 3 puzzle 1
:return: the duplicate item (as a character)
Check if rucksack1 and rucksack2 have an item in common. This will merge the compartments in each rucksack.
:param rucksack1:
:param rucksack2:
:return: list of the duplicate item (as a character) or None if there is no duplicate
"""
for a in rucksack[0]:
if a in rucksack[1]:
r1 = list(set(rucksack1[0] + rucksack1[1]))
r2 = list(set(rucksack2[0] + rucksack2[1]))
dups = []
for a in r1:
if a in r2:
dups.append(a)
return dups
@staticmethod
def check_compartments_for_duplicates(compartment_1: List[str], compartment_2: List[str]) -> str:
"""
Check two given compartment for the duplicate item according to day 3 puzzle 1
:return: the duplicate item (as a character) or None if there is no duplicate
"""
for a in compartment_1:
if a in compartment_2:
return a
raise ValueError(f"Couldn't find ANY duplicate in this rucksack: {rucksack[0]} | {rucksack[1]}")
return False

@ -6,12 +6,33 @@ from entities.elf import Elf
if __name__ == "__main__":
rs = RucksackLoader("./input/day3/input.lst").parse_file()
i = item_priority
duplicate_prios = []
for rucksack in rs:
elf = Elf()
duplicate = elf.check_rucksack_for_compartment_duplicates(rucksack)
duplicate_prios.append(item_priority[duplicate])
print(f"{elf} found duplicate \"{duplicate}\" with priority {item_priority[duplicate]}!")
print()
print(f"The priority sum is {sum(duplicate_prios)}")
groups = []
curr_group = []
for rucksack in rs: # creating groups of 3
elf = Elf()
curr_group.append((elf, rucksack))
if len(curr_group) >= 3:
groups.append(curr_group)
curr_group = []
group_badges = []
for i in range(len(groups)):
group = groups[i]
dup1 = Elf.check_rucksacks_for_duplicates(group[0][1], group[1][1])
dup2 = Elf.check_rucksacks_for_duplicates(group[0][1], group[2][1])
r = Elf.check_compartments_for_duplicates(dup1, dup2)
if r:
final_dup = r
print(f"Group #{i} has the badge item {final_dup} ({item_priority[final_dup]})")
else:
final_dup = None
raise ValueError(f"Found no badge item for Group #{i}")
group_badges.append(final_dup)
print("")
print(f"The priority sum is: {sum([item_priority[c] for c in group_badges])}")

Loading…
Cancel
Save