Day 1 - Advent of Code 2023
Working solutions for the day 1 puzzles.
Part One
""" day_01_01.py """
# usage: python3 day_01_01.py < input
import sys
answer = 0
for line in sys.stdin:
digits = [character for character in line if character.isdigit()]
answer += int(digits[0] + digits[-1])
print(answer)
Part Two
""" day_01_02.py """
# usage: python3 day_01_02.py < input
import sys
def text2digits(text):
""" retain digits while converting digits expressed as words """
ref = {'one': '1', 'two': '2', 'three': '3', 'four': '4',
'five': '5', 'six': '6', 'seven': '7', 'eight': '8',
'nine': '9'}
result = []
for i, character in enumerate(text):
if character.isdigit():
result.append(character)
else:
for word, digit in ref.items():
if text[i:].startswith(word):
result.append(digit)
break
return result
answer = 0
for line in sys.stdin:
digits = text2digits(line)
answer += int(digits[0] + digits[-1])
print(answer)