solution for part a
This commit is contained in:
44
3/rucksack.py
Normal file
44
3/rucksack.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import string
|
||||||
|
|
||||||
|
from aocd.models import Puzzle
|
||||||
|
from aocd import submit
|
||||||
|
|
||||||
|
def eval_priority(rucksack: str) -> int:
|
||||||
|
'''
|
||||||
|
evaluates the priority of a duplicate item
|
||||||
|
'''
|
||||||
|
|
||||||
|
# create a map of priority values
|
||||||
|
priorities = dict(zip(string.ascii_letters, range(1,53)))
|
||||||
|
|
||||||
|
# slize rucksack into compartments
|
||||||
|
size = len(rucksack)
|
||||||
|
a,b = rucksack[:size//2], rucksack[size//2:]
|
||||||
|
|
||||||
|
# find the duplicate item
|
||||||
|
duplicate = ''.join(
|
||||||
|
set(a).intersection(b)
|
||||||
|
)
|
||||||
|
|
||||||
|
# return the priority
|
||||||
|
return priorities[duplicate]
|
||||||
|
|
||||||
|
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 all duplicate items
|
||||||
|
answer = sum([eval_priority(rucksack) for rucksack in supplies])
|
||||||
|
print(f'sum of priorities of all duplicate items: {answer}')
|
||||||
|
submit(answer, part='a', day=3, year=2022)
|
||||||
|
|
||||||
|
# part b:
|
||||||
|
#submit(total, part='b', day=3, year=2022)
|
||||||
Reference in New Issue
Block a user