08_registers.py 744 B

123456789101112131415161718192021222324
  1. #!/usr/bin/env python3
  2. import sys
  3. from operator import add, sub, gt, ge, lt, le, eq, ne
  4. from collections import defaultdict
  5. ops = { 'inc': add, 'dec': sub}
  6. tests = {'>': gt, '>=': ge, '<': lt, '<=': le, '==': eq, '!=': ne}
  7. def parse(f):
  8. for line in f:
  9. inst, cond = line.rstrip().split(' if ')
  10. outreg, op, opnd = inst.split()
  11. treg, t, tconst = cond.split()
  12. yield outreg, ops[op], int(opnd), treg, tests[t], int(tconst)
  13. regs = defaultdict(int)
  14. allmax = 0
  15. for outreg, op, opconst, testreg, test, testconst in parse(sys.stdin):
  16. if test(regs[testreg], testconst):
  17. regs[outreg] = op(regs[outreg], opconst)
  18. curmax = max(regs.values())
  19. allmax = max(allmax, curmax)
  20. print(curmax)
  21. print(allmax)