-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag3.py
32 lines (23 loc) · 855 Bytes
/
tag3.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
import re
with open("input3.txt", "r") as f:
d = [x.rstrip() for x in f.readlines()]
# part 1
# a-z 1-26, ascii a = 97 -> -96
# A-Z 27-52 ascii A = 65 -> -38
prioritySum = 0
for items in d:
firstHalf = items[:int(len(items)/2)]
secondHalf = items[int(len(items)/2):]
identical = re.search(f"[{firstHalf}]", secondHalf)[0]
ascii = ord(identical)
prioritySum += ascii - 96 if ascii >= 97 else ascii - 38
print(prioritySum)
# part 2
badgeSum = 0
for i in range(int(len(d)/3)):
results = re.findall(f"[{d[i*3]}]", d[i*3+1])
matches_1_2 = "".join(results)
badge = re.search(f"[{matches_1_2}]", d[i*3+2])[0]
ascii = ord(badge)
badgeSum += ascii - 96 if ascii >= 97 else ascii - 38
print(badgeSum)