1
0
Fork 0
advent-of-code/2023/day01.py

45 lines
1.2 KiB
Python
Raw Normal View History

2024-02-08 09:55:40 +01:00
#!/usr/bin/env python3
lines = []
with open('./inputs/day01.txt') as f:
lines = f.readlines()
# part 1
calibration_values = []
for line in lines:
digits = []
for ch in line:
if (ch.isdigit()):
digits.append(int(ch))
calibration_values.append(int(f''.join(map(str, [digits[0], digits[-1]]))))
print(calibration_values)
print(sum(calibration_values))
# part 2
calibration_values_with_replace = []
for line in lines:
digits = []
# replace 'one' with 'o1e', 'two' with 't2o', etc.
temp = line.lower()
temp = line.replace('one', 'o1e')
temp = temp.replace('two', 't2o')
temp = temp.replace('three', 'th3ee')
temp = temp.replace('four', 'f4ur')
temp = temp.replace('five', 'f5ve')
temp = temp.replace('six', 's6x')
temp = temp.replace('seven', 'se7en')
temp = temp.replace('eight', 'ei8ht')
temp = temp.replace('nine', 'ni9e')
for ch in temp:
if (ch.isdigit()):
digits.append(int(ch))
calibration_values_with_replace.append(int(f''.join(map(str, [digits[0], digits[-1]]))))
print(calibration_values_with_replace)
print(sum(calibration_values_with_replace))