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

45 lines
No EOL
1.2 KiB
Python

#!/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))