Advertisement
icarussiano

Day 1 part 2 python

Dec 1st, 2023
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. somma=0
  2. d={'one':'1','two':'2','three':'3','four':'4','five':'5','six':'6','seven':'7','eight':'8','nine':'9','1':'1','2':'2','3':'3','4':'4','5':'5','6':'6','7':'7','8':'8','9':'9'}
  3. with open("input") as file:
  4.     data = file.read().splitlines()
  5.     for line in data:
  6.         f = dict.fromkeys(d.keys(), 0)
  7.         rf = dict.fromkeys(d.keys(), 0)
  8.         for c in d.keys():
  9.             f[c] = line.find(c) if line.find(c) != -1 else 100000
  10.             rf[c] = line.rfind(c)
  11.         #primacifra = d[min(f, key=f.get)]
  12.         #secondacifra = d[max(rf, key=rf.get)]
  13.         somma+=10*int(d[min(f, key=f.get)])+int(d[max(rf, key=rf.get)])
  14. print(somma)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement