-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_3.py
58 lines (38 loc) · 1.45 KB
/
day_3.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
from typing import Tuple, List
def read() -> List[str]:
with open("input.txt") as file:
lines = file.readlines()
lines = list(map(lambda s: s.strip("\n"), lines))
return lines
def _get_priority(s: str) -> int:
base_priority = 1 if s.islower() else 27
return ord(s.lower()) - ord("a") + base_priority
def get_total_priority(rucksacks: List[Tuple[str, str]]) -> int:
sum_of_priorities = 0
for rucksack in rucksacks:
compartment_size = len(rucksack) // 2
fst = rucksack[:compartment_size]
snd = rucksack[compartment_size:]
assert len(fst) == len(snd)
in_both = list(set(fst).intersection(snd))
assert len(in_both) == 1
sum_of_priorities += _get_priority(in_both[0])
return sum_of_priorities
def get_total_badge_priority(rucksacks: List[Tuple[str, str]]) -> int:
sum_of_priorities = 0
group_size = 3
for i in range(0, len(rucksacks), group_size):
badge = set(rucksacks[i])
for j in range(1, group_size):
badge = badge.intersection(rucksacks[i + j])
badge = list(badge)[0]
sum_of_priorities += _get_priority(badge)
return sum_of_priorities
def main():
rucksacks = read()
total_prio = get_total_priority(rucksacks)
print(f"Total prio: {total_prio}")
total_badge_prio = get_total_badge_priority(rucksacks)
print(f"Total badge prio: {total_badge_prio}")
if __name__ == "__main__":
main()