add solution to part b
This commit is contained in:
@@ -3,9 +3,9 @@ import string
|
|||||||
from aocd.models import Puzzle
|
from aocd.models import Puzzle
|
||||||
from aocd import submit
|
from aocd import submit
|
||||||
|
|
||||||
def eval_priority(rucksack: str) -> int:
|
def eval_item_priority(rucksack: str) -> int:
|
||||||
'''
|
'''
|
||||||
evaluates the priority of a duplicate item
|
evaluates the priority of a common item
|
||||||
'''
|
'''
|
||||||
|
|
||||||
# create a map of priority values
|
# create a map of priority values
|
||||||
@@ -15,13 +15,28 @@ def eval_priority(rucksack: str) -> int:
|
|||||||
size = len(rucksack)
|
size = len(rucksack)
|
||||||
a,b = rucksack[:size//2], rucksack[size//2:]
|
a,b = rucksack[:size//2], rucksack[size//2:]
|
||||||
|
|
||||||
# find the duplicate item
|
# find the common item
|
||||||
duplicate = ''.join(
|
common = ''.join(
|
||||||
set(a).intersection(b)
|
set(a).intersection(b)
|
||||||
)
|
)
|
||||||
|
|
||||||
# return the priority
|
# return the priority
|
||||||
return priorities[duplicate]
|
return priorities[common]
|
||||||
|
|
||||||
|
def eval_badge_priority(rucksacks: list) -> int:
|
||||||
|
'''
|
||||||
|
evaluate the priority of duplicate items over groups of three rucksacks
|
||||||
|
'''
|
||||||
|
|
||||||
|
# create a map of priority values
|
||||||
|
priorities = dict(zip(string.ascii_letters, range(1,53)))
|
||||||
|
|
||||||
|
# find common item among three sets
|
||||||
|
common = ''.join(
|
||||||
|
set(rucksacks[0]) & set(rucksacks[1]) & set(rucksacks[2])
|
||||||
|
)
|
||||||
|
|
||||||
|
return priorities[common]
|
||||||
|
|
||||||
def parse_input(data: list) -> list:
|
def parse_input(data: list) -> list:
|
||||||
'''
|
'''
|
||||||
@@ -35,10 +50,14 @@ if __name__ == "__main__":
|
|||||||
puzzle = Puzzle(year=2022, day=3)
|
puzzle = Puzzle(year=2022, day=3)
|
||||||
supplies = parse_input(puzzle.input_data)
|
supplies = parse_input(puzzle.input_data)
|
||||||
|
|
||||||
# part a: determine the priority of all duplicate items
|
# part a: determine the priority of dupplicate items in compartments
|
||||||
answer = sum([eval_priority(rucksack) for rucksack in supplies])
|
answer_a = sum([eval_item_priority(rucksack) for rucksack in supplies])
|
||||||
print(f'sum of priorities of all duplicate items: {answer}')
|
print(f'sum of priorities of all common items amongst compartments: {answer_a}')
|
||||||
submit(answer, part='a', day=3, year=2022)
|
submit(answer_a, part='a', day=3, year=2022)
|
||||||
|
|
||||||
# part b:
|
# part b: determine the priority of common items in groups of three
|
||||||
#submit(total, part='b', day=3, year=2022)
|
# split the supply list into groups of three
|
||||||
|
rucksacks = [ supplies[i:i+3] for i in range(0,len(supplies),3) ]
|
||||||
|
answer_b = sum([ eval_badge_priority(rucksack) for rucksack in rucksacks ])
|
||||||
|
print(f'sum of priorities of all common items amongst groups of three: {answer_b}')
|
||||||
|
submit(answer_b, part='b', day=3, year=2022)
|
||||||
|
|||||||
Reference in New Issue
Block a user