This commit is contained in:
basicbonobo 2023-12-01 14:39:52 +01:00 committed by Eivind Øksnevad
parent 9d95020a1b
commit 8aaf59fa13
2 changed files with 25 additions and 2 deletions

20
one.py
View File

@ -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))

7
test1b Normal file
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen