aoc/23/day1/main.py
2023-12-04 10:25:33 +01:00

57 lines
1.3 KiB
Python
Executable file

#!//usr/bin/env python
def get_lines():
lines = list()
with open("input.txt", "r") as f:
for line in f:
lines.append(line.strip())
return lines
total = 0
for line in get_lines():
for char in line:
if char.isdigit():
first = int(char)
break
for char in line[::-1]:
if char.isdigit():
last = int(char)
break
value = first * 10 + last
total += value
print(total)
total = 0
for line in get_lines():
digits = []
for i in range(len(line)):
rest = line[i:]
if rest.startswith("one"):
digits.append(1)
if rest.startswith("two"):
digits.append(2)
if rest.startswith("three"):
digits.append(3)
if rest.startswith("four"):
digits.append(4)
if rest.startswith("five"):
digits.append(5)
if rest.startswith("six"):
digits.append(6)
if rest.startswith("seven"):
digits.append(7)
if rest.startswith("eight"):
digits.append(8)
if rest.startswith("nine"):
digits.append(9)
if rest.startswith("zero"):
digits.append(0)
if rest[0].isdigit():
digits.append(int(rest[0]))
total += digits[0] * 10 + digits[-1]
print(total)