improved day03
This commit is contained in:
parent
c95e1d02ec
commit
3a8ded02ae
1 changed files with 37 additions and 40 deletions
|
|
@ -6,50 +6,47 @@ day_num = 3
|
|||
|
||||
|
||||
def part1(lines: Iterator[str]) -> int:
|
||||
return sum(Rucksack.find_double(line) for line in lines)
|
||||
return sum(priority(find_double(line)) for line in lines)
|
||||
|
||||
|
||||
def part2(lines: Iterator[str]) -> int:
|
||||
groups = [lines] * 3
|
||||
zipped = zip(*groups, strict=True)
|
||||
return sum(Rucksack.find_common(group) for group in zipped)
|
||||
groups = zip(lines, lines, lines, strict=True)
|
||||
return sum(priority(find_common_item(group)) for group in groups)
|
||||
|
||||
|
||||
class Rucksack:
|
||||
@staticmethod
|
||||
def priority(char: str) -> int:
|
||||
"""
|
||||
Returns the priority given to each item.
|
||||
It is assumed, that we are given a valid item
|
||||
"""
|
||||
num = ord(char)
|
||||
if ord('a') <= num and num <= ord('z'):
|
||||
return num - ord('a') + 1
|
||||
if ord('A') <= num and num <= ord('Z'):
|
||||
return num - ord('A') + 27
|
||||
raise Exception(f"Unknown char: {char}")
|
||||
def priority(char: str) -> int:
|
||||
"""
|
||||
Returns the priority given to each item.
|
||||
It is assumed, that we are given a valid item
|
||||
"""
|
||||
if 'a' <= char and char <= 'z':
|
||||
return ord(char) - ord('a') + 1
|
||||
if 'A' <= char and char <= 'Z':
|
||||
return ord(char) - ord('A') + 27
|
||||
raise Exception(f"Unknown char: {char}")
|
||||
|
||||
@staticmethod
|
||||
def find_double(rucksack: str) -> int:
|
||||
"""
|
||||
Finds the priority of the one item in both compartments.
|
||||
It is assumed that there is only one such item
|
||||
"""
|
||||
half = len(rucksack) // 2
|
||||
first = rucksack[:half]
|
||||
second = rucksack[half:]
|
||||
for item in first:
|
||||
if item in second:
|
||||
return Rucksack.priority(item)
|
||||
raise Exception("No double item")
|
||||
|
||||
@staticmethod
|
||||
def find_common(group: tuple[str, ...]) -> int:
|
||||
"""
|
||||
Finds the one item in all three rucksacks given.
|
||||
It is assumed that there is only one such item and group has exactly three rucksacks
|
||||
"""
|
||||
for item in group[0]:
|
||||
if item in group[1] and item in group[2]:
|
||||
return Rucksack.priority(item)
|
||||
raise Exception("No common item found")
|
||||
def find_double(rucksack: str) -> str:
|
||||
"""
|
||||
Finds the one item in both compartments.
|
||||
It is assumed that there is only one such item
|
||||
"""
|
||||
half = len(rucksack) // 2
|
||||
first = rucksack[:half]
|
||||
second = rucksack[half:]
|
||||
for item in first:
|
||||
if item in second:
|
||||
return item
|
||||
raise Exception("No double item")
|
||||
|
||||
|
||||
def find_common_item(group: tuple[str, str, str]) -> str:
|
||||
"""
|
||||
Finds the one item in all three rucksacks given.
|
||||
It is assumed that there is only one such item and group has exactly three rucksacks
|
||||
"""
|
||||
first, second, third = group
|
||||
for item in first:
|
||||
if item in second and item in third:
|
||||
return item
|
||||
raise Exception("No common item found")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue