-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3_rucksack_organization_2.py
More file actions
31 lines (25 loc) · 1.03 KB
/
day3_rucksack_organization_2.py
File metadata and controls
31 lines (25 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import sys
# day 3 part 2 - aoc 2022 https://adventofcode.com/2022/day/3#part2
def main():
file_name = sys.argv[1]
rucksacks = []
with open(file_name, 'r') as file:
rucksacks = file.read().strip('\n').split('\n')
def calc_priority(common_set: set) -> int:
"""
priority 'a' - 'z' -> [1...26]
priority 'A" - 'Z' -> [27..52]
"""
priority = 0
for ch in common_set:
priority += 1 + ord(ch) - ord('a') if ch.islower() else 27 + ord(ch) - ord('A')
return priority
# divide input rucksacks into groups of 3
group_size = 3
groups = [ rucksacks[i * group_size: (i + 1) * group_size ] for i in range( len(rucksacks) // group_size ) ]
# calculate priority on elements in the set intersection for each group
common_sets = ( set.intersection( set(group[0]), set(group[1]), set(group[2]) ) for group in groups )
count = sum( calc_priority(common_set) for common_set in common_sets )
print (count)
if __name__ == "__main__":
main()