You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.4 KiB
44 lines
1.4 KiB
#!/usr/bin/env python3
|
|
|
|
import sys, re
|
|
|
|
|
|
if __name__ == '__main__':
|
|
words = [line.strip('\n') for line in open(sys.argv[1])]
|
|
nums = [int(re.sub('[abcdefghijklmnopqrstuvwxyz]','',line)) for line in words]
|
|
|
|
# challenge 1
|
|
res1 = 0
|
|
for n in nums:
|
|
if 9 < n < 100: res1 += n
|
|
elif n < 10: res1 += n * 11
|
|
else: res1 += int(''.join(re.findall(r'(\d).*(\d)',str(n))[0]))
|
|
|
|
res1 = str(res1)
|
|
print("challenge 1:" + "\n" + res1 + "\n")
|
|
|
|
# challenge 2
|
|
|
|
## reformat, but keep in mind there may be overlap!!
|
|
words = [re.sub('one','o1e',line) for line in words]
|
|
words = [re.sub('two','t2o',line) for line in words]
|
|
words = [re.sub('three','t3e',line) for line in words]
|
|
words = [re.sub('four','f4r',line) for line in words]
|
|
words = [re.sub('five','f5e',line) for line in words]
|
|
words = [re.sub('six','s6x',line) for line in words]
|
|
words = [re.sub('seven','s7n',line) for line in words]
|
|
words = [re.sub('eight','e8t',line) for line in words]
|
|
words = [re.sub('nine','n9e',line) for line in words]
|
|
|
|
## same as above
|
|
nums = [int(re.sub('[abcdefghijklmnopqrstuvwxyz]','',line)) for line in words]
|
|
|
|
res2 = 0
|
|
for n in nums:
|
|
if 9 < n < 100: res2 += n
|
|
elif n < 10: res2 += n * 11
|
|
else: res2 += int(''.join(re.findall(r'(\d).*(\d)',str(n))[0]))
|
|
|
|
res2 = str(res2)
|
|
print("challenge 2:" + "\n" + res2 + "\n")
|
|
|
|
|