-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_rucksack-reorganization.py
executable file
·60 lines (44 loc) · 1.72 KB
/
03_rucksack-reorganization.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
#
# Copyright (c) 2022, xphade <github.com/xphade>
# SPDX-License-Identifier: MIT
import string
import time
from typing import List
from aoc_utils import get_input_path, print_elapsed_time
def get_priority(character: str) -> int:
return string.ascii_letters.index(character) + 1
def calculate_sum_of_item_type_priorities(item_lists: List[str]) -> int:
sum_of_priorities = 0
for item_list in item_lists:
split_index = len(item_list) // 2
common_items = set(item_list[:split_index]).intersection(
item_list[split_index:]
)
assert len(common_items) == 1
sum_of_priorities += get_priority(common_items.pop())
return sum_of_priorities
def calculate_sum_of_badge_priorities(item_lists: List[str]) -> int:
sum_of_priorities = 0
for idx in range(0, len(item_lists), 3):
common_items = (
set(item_lists[idx])
.intersection(item_lists[idx + 1])
.intersection(item_lists[idx + 2])
)
assert len(common_items) == 1
sum_of_priorities += get_priority(common_items.pop())
return sum_of_priorities
def main():
data_path = get_input_path("Day 03: Rucksack Reorganization")
with open(data_path, "r") as file:
item_lists = file.read().splitlines()
start = time.monotonic()
sum_of_type_priorities = calculate_sum_of_item_type_priorities(item_lists)
sum_of_badge_priorities = calculate_sum_of_badge_priorities(item_lists)
stop = time.monotonic()
print(f"Sum of common item type priorities: {sum_of_type_priorities}")
print(f"Sum of badge priorities: {sum_of_badge_priorities}")
print_elapsed_time(start, stop)
if __name__ == "__main__":
main()