aoc/2023/01/01.py

25 lines
781 B
Python
Raw Normal View History

2024-03-05 09:32:18 +00:00
with open("input.txt") as f:
lines = f.readlines()
def extract_numbers(line: str) -> int:
nums = [c for c in line if c in "1234567890"]
return int(nums[0] + nums[-1])
part1 = sum(map(extract_numbers, lines))
print(part1)
def extract_numbers2(line: str) -> int:
# we need to preserve overlaps in both directions
line = line.replace("one", "one1one")
line = line.replace("two", "two2two")
line = line.replace("three", "three3three")
line = line.replace("four", "four4four")
line = line.replace("five", "five5five")
line = line.replace("six", "six6six")
line = line.replace("seven", "seven7seven")
line = line.replace("eight", "eight8eight")
line = line.replace("nine", "nine9nine")
return extract_numbers(line)
part2 = sum(map(extract_numbers2, lines))
print(part2)