aoc2023/d01/run.py

17 lines
434 B
Python

import sys
D = [str(i) for i in range(1,10)]
if sys.argv[1] == "2":
D += ['one','two','three','four','five','six','seven','eight','nine']
def process(l):
k = list()
for p in range(len(l)):
for i,d in enumerate(D):
if l[p:p+len(d)] == d:
k.append(i%9+1)
break
return k[0]*10 + k[-1] if len(k)>0 else 0
print(sum(process(l) for l in sys.stdin.read().splitlines()))