black formatter

This commit is contained in:
aaron
2022-12-03 14:42:25 +01:00
parent 36deee750f
commit 27147cd9bd

View File

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