0
0
Fork 0

Add solutions for day 3 in Python

This commit is contained in:
Oliver Akins 2022-12-03 10:17:44 -06:00
parent 808a63f32e
commit 6e4472386e
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99
2 changed files with 65 additions and 0 deletions

34
day_03/python/part_1.py Normal file
View file

@ -0,0 +1,34 @@
import sys
def find_common(*args):
i = set.intersection(*map(set, args))
if len(i) == 1:
return i.pop()
return None
def split_str(s):
mid = len(s)//2
return (s[:mid], s[mid:])
def determine_priority(letter):
v = ord(letter)
if 97 <= v <= 122:
return v - 96
elif 65 <= v <= 90:
return v - 38
def run():
if len(sys.argv) < 2:
print("Not enough arguments")
return
total = 0
with open(sys.argv[1], "r") as f:
for line in f:
l = line.strip()
s1, s2 = split_str(l)
x = find_common(s1, s2)
total += determine_priority(x)
print(f"Sum: {total}")
if __name__ == "__main__":
run()

31
day_03/python/part_2.py Normal file
View file

@ -0,0 +1,31 @@
import sys
def find_common(*args):
i = set.intersection(*map(set, args))
if len(i) == 1:
return i.pop()
return None
def determine_priority(letter):
v = ord(letter)
if 97 <= v <= 122:
return v - 96
elif 65 <= v <= 90:
return v - 38
def run():
if len(sys.argv) < 2:
print("Not enough arguments")
return
total = 0
with open(sys.argv[1], "r") as f:
for line in f:
l = line.strip()
l2 = f.readline().strip()
l3 = f.readline().strip()
x = find_common(l, l2, l3)
total += determine_priority(x)
print(f"Sum: {total}")
if __name__ == "__main__":
run()