-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday01.py
More file actions
90 lines (65 loc) · 1.68 KB
/
day01.py
File metadata and controls
90 lines (65 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from pathlib import Path
from loguru import logger
from typer import Typer
from utils import timer
main = Typer()
def calibration(line: str) -> int:
"""Compute calibration
Identify first and larg integer in the string, multiplying the first by 10
and summing them.
"""
if len(line.strip()) == 0:
return 0
first: int | None = None
last: int = -1
for c in line:
if c.isdigit():
first = first or int(c)
last = int(c)
return (first or 0) * 10 + last
spelling = [
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
]
def replace_letter_numbers(line: str) -> str:
"""Replace written numbers by digits
oneight is interpreted as 18
"""
res = []
for offset in range(len(line)):
if line[offset].isdigit():
res.append(line[offset])
continue
for i, number in enumerate(spelling):
if line[offset:].startswith(number):
res.append(str(i + 1))
return "".join(res)
@timer
def task01(input: str) -> int:
"""Task 01 solution"""
return sum(calibration(line) for line in input.split("\n"))
@timer
def task02(input: str) -> int:
"""Task 02 solution"""
return sum(calibration(replace_letter_numbers(line)) for line in input.split("\n"))
@main.command()
def entrypoint(path: Path):
"""Entrypoint reading the input
Args:
path: Path to input file
"""
with open(path) as f:
input = f.read()
logger.info("Task 01")
logger.info(task01(input))
logger.info("Task 02")
logger.info(task02(input))
if __name__ == "__main__":
main()