This commit is contained in:
parent
9d95020a1b
commit
8aaf59fa13
20
one.py
20
one.py
|
@ -5,8 +5,24 @@ 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]
|
||||
|
||||
sums = [int(str(n[0]) + str(n[-1])) for n in ns]
|
||||
print(sum(sums))
|
||||
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))
|
||||
|
|
Loading…
Reference in New Issue