23_coprocessor.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python3
  2. import sys
  3. def count_mul(program):
  4. def get(arg):
  5. return int(arg) if arg.isdigit() or arg.startswith('-') else regs[arg]
  6. regs = {'a': 0, 'h': 0}
  7. muls = 0
  8. pc = 0
  9. while pc < len(program):
  10. opcode, a, b = program[pc]
  11. if opcode == 'set':
  12. regs[a] = get(b)
  13. elif opcode == 'sub':
  14. regs[a] -= get(b)
  15. elif opcode == 'mul':
  16. muls += 1
  17. regs[a] *= get(b)
  18. elif opcode == 'jnz':
  19. if get(a):
  20. pc += get(b) - 1
  21. pc += 1
  22. return muls
  23. def primes_sieve(n):
  24. sieve = [1] * n
  25. for i in range(2, n):
  26. if sieve[i]:
  27. for m in range(i * i, n, i):
  28. sieve[m] = 0
  29. return sieve
  30. def compute_h():
  31. # This emulates the program, which computes the number of non-primes between
  32. # b and c at intervals of 17
  33. b = 109900
  34. c = b + 17000 + 1
  35. is_prime = [1] * c
  36. for i in range(2, c):
  37. if is_prime[i]:
  38. for m in range(i * i, c, i):
  39. is_prime[m] = 0
  40. return sum(1 - is_prime[i] for i in range(b, c, 17))
  41. program = [line.split() for line in sys.stdin]
  42. print(count_mul(program))
  43. print(compute_h())