-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.py
46 lines (23 loc) · 800 Bytes
/
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
sum_1 = 0
sum_2 = 0
data = []
priorities = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
with open('data/3.txt') as file:
for line in file:
data.append(line)
# task 1: find sum of priorities for the common element in both halves of each rucksack
for rucksack in data:
half = len(rucksack) // 2
for item in rucksack[:half]:
if item in rucksack[half:]:
sum_1 += priorities.index(item) + 1
break
# task 2: find sum of priorities for the common element in groups of there rucksacks
for i in range(0, len(data), 3):
group = data[i:i+3]
for item in group[0]:
if item in group[1] and item in group[2]:
sum_2 += priorities.index(item) + 1
break
print('Task 1:', sum_1)
print('Task 2:', sum_2)