Skip to content

Instantly share code, notes, and snippets.

@munguial
Last active December 8, 2023 17:38
Show Gist options
  • Save munguial/55fbfd9cb0a6bec0e954e2e7d65ba35e to your computer and use it in GitHub Desktop.
Save munguial/55fbfd9cb0a6bec0e954e2e7d65ba35e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
numbers = {"zero": 0,
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9}
def getFirstDigit(line: str, reverse: bool) -> str:
indices = reversed(range(len(line))) if reverse else range(len(line))
for c in indices:
if line[c].isdigit():
return line[c]
for i in range(0, 5):
if c + i < len(line) and line[c : c + i + 1] in numbers:
return str(numbers[line[c : c + i + 1]])
def getCalibrationValue(line: str) -> int:
return int(getFirstDigit(line, False) + getFirstDigit(line, True))
result = 0
for line in sys.stdin:
result += getCalibrationValue(line)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment