-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
77 lines (58 loc) · 1.96 KB
/
solution.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Advent of Code 2022: day 3
"""
import os
from more_itertools import chunked
input_file = os.path.join(os.path.dirname(__file__), 'input.txt')
split_input = open(input_file).read().splitlines()
def type_to_priority(_type: str) -> int:
"""
Determines the priority of a type (single character)
:param _type: Single uppercase or lowercase character.
:return: The priority of the type.
"""
# Lowercase type
if _type.islower():
return ord(_type) - ord('a') + 1
# Uppercase type
return ord(_type) - ord('A') + 27
def split_rucksack(rucksack: str) -> tuple[str, str]:
"""
Splits a rucksack down the middle into two parts.
:param rucksack: The rucksack string representation.
:return: The two compartments of the rucksack.
"""
# Determine boundary of compartments
splitter = len(rucksack) // 2
# Return the two individual compartments
return rucksack[:splitter], rucksack[splitter:]
def get_duplicate_type(rucksack: str) -> str:
"""
Gets the type that is present in both rucksack compartments.
:param rucksack: The rucksack.
:return: The type present in both compartments.
"""
# Split the rucksack into two compartments
x, y = split_rucksack(rucksack)
# Determine the common type
return list(set(x) & set(y))[0]
def get_common_type(rucksacks: list[str]) -> str:
"""
Gets the type that is present in all rucksacks.
:param rucksacks: The rucksacks of a group.
:return: The type present in all rucksacks.
"""
# Extract each rucksack individually
x, y, z = [set(rucksack) for rucksack in rucksacks]
# Determine the common type
return list(set(x) & set(y) & set(z))[0]
solution = sum([
type_to_priority(get_duplicate_type(rucksack))
for rucksack in split_input
])
print(f'part1: {solution}')
solution = sum([
type_to_priority(get_common_type(rucksacks))
for rucksacks in chunked(split_input, 3)
])
print(f'part2: {solution}')