1.py (1090B) [raw]
1 #!/usr/bin/env python3 2 import sys 3 4 def expand(dense): 5 fs = [] 6 for i in range(len(dense)): 7 if i % 2 == 0: 8 for j in range(int(dense[i])): 9 fs.append(i // 2) 10 else: 11 for j in range(int(dense[i])): 12 fs.append(None) 13 return fs 14 15 def pprint(fs): 16 for c in fs: 17 if c is not None: 18 print(c, end="") 19 else: 20 print(".", end="") 21 print("") 22 23 def compact(fs): 24 comp = [c for c in fs] 25 l = 0 26 r = len(fs) - 1 27 while l < r: 28 while comp[l] is not None: 29 l += 1 30 while comp[r] is None: 31 r -= 1 32 if l < r: 33 comp[l] = comp[r] 34 comp[r] = None 35 l += 1 36 r -= 1 37 38 return comp 39 40 def chksum(fs): 41 tot = 0 42 for i in range(len(fs)): 43 if fs[i] is None: 44 break 45 tot += fs[i] * i 46 47 return tot 48 49 if __name__ == '__main__': 50 dense = next(sys.stdin).strip() 51 fs = expand(dense) 52 comp = compact(fs) 53 #pprint(fs) 54 #pprint(compact(fs)) 55 print(chksum(comp)) 56