01.1, 01.2

This commit is contained in:
2023-12-01 15:31:55 +03:00
parent d5fe0b527c
commit 34740e6c4c
2 changed files with 1033 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
digits = dict(zip(["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"], map(str, range(1, 10))))
inp = open("input.txt").readlines()
print(sum(int(x[0]+x[-1]) for line in inp if (x:=list(filter(str.isdigit, line)))))
def rep(line):
left, right = None, None
for word, num in digits.items():
l = line.find(word)
if l!=-1:
if left is None:
left = l
lword = word
elif l<left:
left = l
lword = word
r = line.rfind(word)
if r!=-1:
if right is None:
right = r
rword = word
elif r>right:
right = r
rword = word
if left is None or right is None:
return line
line = line[:left]+digits[lword]+line[left:]
line = line[:right+len(rword)]+digits[rword]+line[right+len(rword):]
return line
print(sum(int(x[0]+x[-1]) for line in inp if (x:=list(filter(str.isdigit, rep(line))))))