https://adventofcode.com/2023ar dalyvausit šiais metais virai ? dalinames sprendimais ~~
1 diena :
import re
sum = 0
i = 1
spellednumbers = [['zero', '0'], ['one', '1'], ['two', '2'], ['three', '3'], ['four', '4'], ['five', '5'], ['six', '6'], ['seven','7'], ['eight', '8'], ['nine', '9']]
numberRegex = r'\d|one|two|three|four|five|six|seven|eight|nine'
with open('input.txt') as inputFile:
f = open("output1.txt", "w")
for line in inputFile:
matchBuffer = []
for match in re.finditer(numberRegex, line):
s = match.start()
e = match.end()
matchBuffer.append(line[s:e])
if line[s:e].isnumeric() == False:
for spelledNumber in spellednumbers:
number = spelledNumber[0]
overlappingIndex = e - 1
overlapped = line.find(number, overlappingIndex, overlappingIndex + len(number))
if overlapped != -1:
sub = line[overlapped:overlapped + len(number)]
matchBuffer.append(sub)
break
firstDig = matchBuffer[0]
secondDig = matchBuffer[len(matchBuffer) - 1]
for index, number in enumerate(spellednumbers):
if firstDig in number:
firstDig = str(index)
break
for index, number in enumerate(spellednumbers):
if secondDig in number:
secondDig = str(index)
break
result = firstDig + secondDig
sum+=int(result)
i+=1
f.write("["+ line.strip() + "] -> " + str(matchBuffer) + " -> " + result + "\n")
f.write("sum of calibration values: " + str(sum))
f.close()