day 2

mail@pastecode.io avatar
unknown
python
6 months ago
628 B
41
Indexable
Never
import re

def get_nums(line: str):
    MAP_DIGITS = {
        "one" : '1',  "two" :  '2', "three": '3',
        "four": '4',  "five":  '5', "six"  : '6',
        "seven": '7', "eight": '8', "nine" : '9'
        }
    
    PATTERN = '(?=(one|two|three|four|five|six|seven|eight|nine|\d))'
    digits = [MAP_DIGITS.get(i,i) for i in re.findall(PATTERN, line)]

    return int(''.join([digits[0],digits[-1]]))

def solve(path: str) -> int:
    with open(path, 'r') as f:
        lines = f.read().splitlines()
        return sum([*map(get_nums, lines)])
    
assert solve('./data/02_test.txt') == 281

print(solve('./data/01.txt'))
Leave a Comment