Skip to content

Commit e13814d

Browse files
committed
Add function that converts digits names to numbers
1 parent bc082d3 commit e13814d

File tree

1 file changed

+14
-0
lines changed
  • puzzles/solutions/2023/d01

1 file changed

+14
-0
lines changed

puzzles/solutions/2023/d01/p2.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@
1111
)
1212

1313

14+
def _convert_digits_names_to_numbers(input_text: str) -> str:
15+
new_text = ""
16+
for line in input_text.splitlines():
17+
new_line = line[0]
18+
for i in range(1, len(line)):
19+
current_part_of_line = line[: i + 1]
20+
new_line += line[i]
21+
for digit_name in DIGITS_NAMES_TO_NUMBERS:
22+
if current_part_of_line.endswith(digit_name):
23+
new_line += DIGITS_NAMES_TO_NUMBERS[digit_name]
24+
new_text += new_line + "\n"
25+
return new_text
26+
27+
1428
def get_answer(input_text: str):
1529
input_text = input_text.translate(DIGITS_NAMES_TO_NUMBERS_TRANSLATION_TABLE)
1630
return p1.calculate_calibration_values_sum(input_text)

0 commit comments

Comments
 (0)