-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.py
executable file
·30 lines (25 loc) · 915 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
#!/usr/bin/python3
# run it as
# ./3.py [ --one | --two ] < inputs/3
import sys
def value(value):
return ord(value) - 96 if value.islower() else ord(value) - 38
if '--one' in sys.argv:
total = 0
for line in sys.stdin:
half_length = len(line) // 2
half1, half2 = line[:half_length], line[half_length:]
# I guess the idea here was to utilize the fact that exactly one item is mismatched
# So I could have used less computationally-intensive code than next line
common = (set(half1) & set(half2)).pop()
total += value(common)
print(total)
elif '--two' in sys.argv:
total = 0
while True:
three_packs = [sys.stdin.readline() for x in range(3)]
if len(three_packs[0]) == 0:
break
sets = [set(line.strip()) for line in three_packs]
total += value((sets[0] & sets[1] & sets[2]).pop())
print(total)