-
Notifications
You must be signed in to change notification settings - Fork 0
/
day03.py
47 lines (36 loc) · 1.48 KB
/
day03.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
import string
from typing import Tuple
def solve(input_data: str) -> Tuple[int, int]:
sum_1 = 0
sum_2 = 0
rucksack_group = []
for line in input_data.splitlines():
chunk_len = len(line) // 2
compartment_a, compartment_b = line[:chunk_len], line[chunk_len:]
# Part 1
for item_type in compartment_a:
if item_type in compartment_b:
sum_1 += string.ascii_letters.index(item_type) + 1
break
# Part 2
rucksack_group.append(line)
if len(rucksack_group) == 3:
largest_rucksack_idx = rucksack_group.index(max(rucksack_group))
idx_a, idx_b = (
(largest_rucksack_idx % 3) - 1, (largest_rucksack_idx % 3) - 2)
for item_type in rucksack_group[largest_rucksack_idx]:
rucksacks = (rucksack_group[idx_a], rucksack_group[idx_b])
if all(item_type in rucksack for rucksack in rucksacks):
sum_2 += string.ascii_letters.index(item_type) + 1
rucksack_group.clear()
break
return (sum_1, sum_2)
if __name__ == '__main__':
example_input = (
'vJrwpWtwJgWrhcsFMMfFFhFp\n'
'jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL\n'
'PmmdzqPrVvPwwTWBwg\nwMqvLMZHhHMvwLHjbvcjnnSBnvTQFn\n'
'ttgJtRGJQctTZtZT\nCrZsJsPPZsGzwwsLwLmpwMDw\n')
expected_sum_1 = 157
expected_sum_2 = 70
assert solve(example_input) == (expected_sum_1, expected_sum_2)