29 lines
797 B
Python
Executable File
29 lines
797 B
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
from useful import *
|
|
|
|
def digits(l): # usually what we want really
|
|
return [int(x) for x in re.findall(r'-?[0-9]', l)]
|
|
|
|
digitz={j: i for i, j in enumerate('zero one two three four five six seven eight nine'.split())}
|
|
def eent(s):
|
|
try:
|
|
return int(s)
|
|
except:
|
|
return digitz[s]
|
|
|
|
def twogits(l):
|
|
return [eent(x) for x in re.findall(r'(?=(zero|one|two|three|four|five|six|seven|eight|nine|[0-9]))', l)]
|
|
|
|
ls = lines(open(0)) # pretty much every time, yeah
|
|
ns = [digits(l) for l in ls]
|
|
twons = [twogits(l) for l in ls]
|
|
|
|
try: # crashes on example for part 2
|
|
sums = [int(str(n[0]) + str(n[-1])) for n in ns]
|
|
print(sum(sums))
|
|
except:
|
|
print('missing digits or whatever')
|
|
twums = [int(str(n[0]) + str(n[-1])) for n in twons]
|
|
print(sum(twums))
|