-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
32 lines (28 loc) · 958 Bytes
/
day3.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
def calculatePriority(lines):
sum = 0
for l in lines:
l = l.strip()
first_half = set(l[:len(l) // 2])
second_half = set(l[len(l) // 2:])
intersection = first_half.intersection(second_half)
p = intersection.pop()
if 'A' <= p <= 'Z':
sum += ord(p) - 65 + 1 + 26
if 'a' <= p <= 'z':
sum += ord (p) - 97 + 1
return sum
def calculateGroupPriority(lines):
sum = 0
for i in range(0, len(lines), 3):
intersection = set(lines[i].strip()).intersection(set(lines[i+1].strip())).intersection(lines[i+2].strip())
p = intersection.pop()
if 'A' <= p <= 'Z':
sum += ord(p) - 65 + 1 + 26
if 'a' <= p <= 'z':
sum += ord (p) - 97 + 1
return sum
if __name__ == '__main__':
f = open('./day3_input.txt', 'r')
lines = f.readlines()
print(calculatePriority(lines))
print(calculateGroupPriority(lines))