62 lines
983 B
Python
62 lines
983 B
Python
#!/usr/bin/env python3
|
|
|
|
from typing import TextIO
|
|
import re
|
|
|
|
# Part 2: Overlaps
|
|
a = {
|
|
"one": "o1e",
|
|
"two": "t2o",
|
|
"three": "t3e",
|
|
"four": "f4r",
|
|
"five": "f5e",
|
|
"six": "s6x",
|
|
"seven": "s7n",
|
|
"eight": "e8t",
|
|
"nine": "n9e",
|
|
"ten": "t10e",
|
|
}
|
|
|
|
|
|
def calc(line: str) -> int:
|
|
e = re.findall(r"\d", line)
|
|
|
|
try:
|
|
first, *_, last = e
|
|
except ValueError:
|
|
first, last = e[0], e[0]
|
|
|
|
return int("".join([first, last]))
|
|
|
|
|
|
def part1(f: TextIO) -> int:
|
|
values: int = 0
|
|
|
|
for line in f:
|
|
line = line.strip()
|
|
|
|
values += calc(line)
|
|
|
|
return values
|
|
|
|
|
|
def part2(f: TextIO) -> int:
|
|
values: int = 0
|
|
|
|
for line in f:
|
|
line = line.strip()
|
|
|
|
for w, r in a.items():
|
|
line = line.replace(w, r)
|
|
|
|
values += calc(line)
|
|
|
|
return values
|
|
|
|
|
|
if __name__ == "__main__":
|
|
with open("day01.txt") as f:
|
|
print(part1(f))
|
|
with open("day01.txt") as f:
|
|
print(part2(f))
|