Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

28 lines
712B

  1. import re
  2. def hush(string):
  3. cv = 0
  4. for c in string:
  5. cv += ord(c)
  6. cv *= 17
  7. cv %= 256
  8. return cv
  9. text = open(0).read()
  10. ans1 = sum(hush(step) for step in text.replace('\n', '').split(','))
  11. print(ans1)
  12. cases = [[] for _ in range(256)]
  13. for label, op, val in re.findall(r'(\w+)(=|-)(\d*)', text):
  14. box = cases[hush(label)]
  15. old = [idx for idx, it in enumerate(box) if it[0] == label]
  16. match old, op, int(val or 0):
  17. case [idx], '-', 0: box.pop(idx)
  18. case [], '=', n: box.append([label, n])
  19. case [idx], '=', n: box[idx] = [label, n]
  20. ans2 = sum(i * j * lens[1] for i, slots in enumerate(cases, 1) for j, lens in enumerate(slots, 1))
  21. print(ans2)