61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
import string
|
|
|
|
from aocd.models import Puzzle
|
|
from aocd import submit
|
|
|
|
# create a map of priority values
|
|
priorities = dict(zip(string.ascii_letters, range(1, 53)))
|
|
|
|
|
|
def eval_item_priority(rucksack: str) -> int:
|
|
"""
|
|
evaluates the priority of a common item
|
|
"""
|
|
|
|
# slize rucksack into compartments
|
|
size = len(rucksack)
|
|
a, b = rucksack[: size // 2], rucksack[size // 2 :]
|
|
|
|
# find the common item
|
|
common = "".join(set(a).intersection(b))
|
|
|
|
# return the priority
|
|
return priorities[common]
|
|
|
|
|
|
def eval_badge_priority(rucksacks: list) -> int:
|
|
"""
|
|
evaluate the priority of duplicate items over groups of three rucksacks
|
|
"""
|
|
|
|
# find common item amongst three sets
|
|
common = "".join(set(rucksacks[0]) & set(rucksacks[1]) & set(rucksacks[2]))
|
|
|
|
# convert to priority list
|
|
return priorities[common]
|
|
|
|
|
|
def parse_input(data: list) -> list:
|
|
"""
|
|
parses the input data and generates a list
|
|
"""
|
|
# split move set into a list of rounds
|
|
return [move for move in data.split("\n")]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# get puzzle and parse data
|
|
puzzle = Puzzle(year=2022, day=3)
|
|
supplies = parse_input(puzzle.input_data)
|
|
|
|
# part a: determine the priority of dupplicate items in compartments
|
|
answer_a = sum([eval_item_priority(rucksack) for rucksack in supplies])
|
|
print(f"sum of priorities of all common items amongst compartments: {answer_a}")
|
|
submit(answer_a, part="a", day=3, year=2022)
|
|
|
|
# part b: determine the priority of common items in groups of three and create a priority summary
|
|
groups = [supplies[i : i + 3] for i in range(0, len(supplies), 3)]
|
|
answer_b = sum([eval_badge_priority(group) for group in groups])
|
|
print(f"sum of priorities of all common items amongst groups of three: {answer_b}")
|
|
submit(answer_b, part="b", day=3, year=2022)
|