adventofcode-2023/01/part02.py
Peter Hudec 47ada5671b day01
2023-12-01 09:05:08 +01:00

38 lines
598 B
Python

#!/usr/bin/env python
from sys import stdin
import re
nums = {
'one': '1',
'two': '2',
'three': '3',
'four': '4',
'five': '5',
'six': '6',
'seven': '7',
'eight': '8',
'nine': '9'
}
def read_line():
for line in stdin:
yield line.strip()
def check_number_start(line):
for k,v in nums.items():
if line.startswith(k):
return v
return None
sum = 0
for line in read_line():
for i in range(len(line)):
f = check_number_start(line[i:])
if f is not None:
line = line[:i] + f + line[i + 1:]
digits = re.sub("[^\d]", "", line)
sum += int(digits[0] + digits[-1])
print(sum)