Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3. def intify(registers, to_intify):
  4. intified = None
  5. try:
  6. intified = int(to_intify)
  7. except ValueError:
  8. intified = registers[to_intify]
  9. return intified
  10.  
  11. def exec_instructions(instructions, registers=None, cur_instr_ind=0, msg_queue=None, prog_id=0):
  12. if registers is None:
  13. registers = defaultdict(int)
  14. to_send = []
  15. # last_sound_played = None
  16. stuck = True
  17. while cur_instr_ind >= 0 and cur_instr_ind < len(instructions):
  18. # print 'registers', registers
  19. cur_instr = instructions[cur_instr_ind]
  20. # print 'prog', prog_id, 'executing', cur_instr
  21. if cur_instr[0] == 'snd':
  22. intified = intify(registers, cur_instr[1])
  23. to_send.append(intified)
  24.  
  25. # last_sound_played = intified
  26. elif cur_instr[0] == 'rcv':
  27. if msg_queue:
  28. registers[cur_instr[1]] = msg_queue.pop(0)
  29. else:
  30. return cur_instr_ind, to_send, stuck
  31.  
  32. # if registers[cur_instr[1]] != 0:
  33. # return last_sound_played
  34. elif cur_instr[0] == 'jgz':
  35. intified = intify(registers, cur_instr[2])
  36. if registers[cur_instr[1]] > 0:
  37. stuck = False
  38. cur_instr_ind += intified
  39. continue
  40. else:
  41. intified = intify(registers, cur_instr[2])
  42.  
  43. if cur_instr[0] == 'set':
  44. registers[cur_instr[1]] = intified
  45. elif cur_instr[0] == 'add':
  46. registers[cur_instr[1]] += intified
  47. elif cur_instr[0] == 'mul':
  48. registers[cur_instr[1]] *= intified
  49. elif cur_instr[0] == 'mod':
  50. registers[cur_instr[1]] = registers[cur_instr[1]] % intified
  51.  
  52. stuck = False
  53. cur_instr_ind += 1
  54.  
  55. return None, to_send, True
  56.  
  57. def exec_programs(instructions, num_programs=2):
  58. all_regs = [defaultdict(int) for _ in range(num_programs)]
  59.  
  60. all_regs[0]['p'] = 0
  61. all_regs[1]['p'] = 1
  62.  
  63. deadlocked_time = [0 for _ in range(num_programs)]
  64. instr_inds = [0 for _ in range(num_programs)]
  65. msg_queues = [[] for _ in range(num_programs)]
  66. prog_one_sent = 0
  67. # while False in deadlocked:
  68. while any([t < 1 for t in deadlocked_time]):
  69. print '\ndeadlocked_time', deadlocked_time
  70. print 'instr_inds', instr_inds
  71. print 'registers 0', all_regs[0]
  72. print 'registers 1', all_regs[1]
  73. # print 'msg queues 0', msg_queues[0]
  74. # print 'msg queues 1', msg_queues[1]
  75.  
  76. for i in range(num_programs):
  77. dest_ind = 0 if i == 1 else 1
  78. if instr_inds[i] is None:
  79. deadlocked[i] = True
  80. continue
  81.  
  82. new_instr_ind, to_send, cur_deadlocked = exec_instructions(instructions, all_regs[i], instr_inds[i], msg_queues[i])
  83. if to_send:
  84. # print 'prog', i, 'sending', to_send
  85. if i == 1:
  86. prog_one_sent += len(to_send)
  87. msg_queues[dest_ind] += to_send
  88.  
  89. # if i == 1:
  90. # prog_one_sent += 1
  91. # msg_queues[dest_ind].append(to_send)
  92.  
  93. if not cur_deadlocked:
  94. deadlocked_time[i] = 0
  95. else:
  96. deadlocked_time[i] += 1
  97.  
  98. if new_instr_ind is not None:
  99. instr_inds[i] = new_instr_ind
  100.  
  101. print '\nfinal deadlocked time', deadlocked_time
  102. return prog_one_sent
  103.  
  104. instructions = []
  105. with open('q18input.txt', 'r') as f:
  106. for line in f:
  107. instructions.append(line.strip().split())
  108.  
  109. # print exec_instructions(instructions)
  110. # assert exec_instructions(instructions) == 2951
  111. print exec_programs(instructions)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement