Skip to content

Commit

Permalink
Docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
gijswobben committed Dec 4, 2023
1 parent fd405ba commit 9d22ff5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/aoc/year_2023/day_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
}


def to_digits(line: str) -> list[int]:
def to_digits(
line: str,
translate_written_digits: bool = False,
) -> list[int]:
"""Convert a line to a list of digits.
Args:
----
line (str): The input line.
translate_written_digits (bool): Whether to translate written digits to
digits.
Returns:
-------
Expand All @@ -34,7 +39,7 @@ def to_digits(line: str) -> list[int]:
for index, char in enumerate(line):
if char.isdigit():
digits.append(int(char))
else:
elif translate_written_digits:
for word, digit in WRITTEN_DIGITS.items():
if line[index : index + len(word)] == word:
digits.append(digit)
Expand Down Expand Up @@ -68,7 +73,11 @@ def part_two(input_lines: list[str]) -> int:
-------
int: The result for assignment two.
"""
return part_one(input_lines)
digits_per_line = [
to_digits(line, translate_written_digits=True) for line in input_lines
]
digits: list[int] = [int(f"{line[0]}{line[-1]}") for line in digits_per_line]
return sum(digits)


if __name__ == "__main__":
Expand Down

0 comments on commit 9d22ff5

Please sign in to comment.