-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_01.py
More file actions
48 lines (33 loc) · 1.34 KB
/
Day_01.py
File metadata and controls
48 lines (33 loc) · 1.34 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
from time import perf_counter
start_time = perf_counter()
NUMBERS_LIST = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
def open_file(file_name="Day_01.txt"):
with open(file_name) as f:
return f.read()
def get_first_n(line: str, part_two: bool = False) -> int:
for index, character in enumerate(line):
if character.isnumeric():
return int(character)
if part_two:
for i, spelled_number in enumerate(NUMBERS_LIST):
if line[index:].startswith(spelled_number):
return i + 1
def get_last_n(line: str, part_two: bool = False) -> int:
for index, character in enumerate(line[::-1]):
if character.isnumeric():
return int(character)
if part_two:
for i, spelled_number in enumerate(NUMBERS_LIST):
if line[: len(line) - index].endswith(spelled_number):
return i + 1
def main():
input_str = open_file()
result_1, result_2 = 0, 0
for line in input_str.splitlines():
result_1 += 10 * get_first_n(line) + get_last_n(line)
result_2 += 10 * get_first_n(line, True) + get_last_n(line, True)
print("Part 1: ", result_1)
print("Part 2: ", result_2)
if __name__ == "__main__":
main()
print("Time elapsed: ", perf_counter() - start_time)