Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- def intify(registers, to_intify):
- intified = None
- try:
- intified = int(to_intify)
- except ValueError:
- intified = registers[to_intify]
- return intified
- def exec_instructions(instructions, registers=None, cur_instr_ind=0, msg_queue=None, prog_id=0):
- if registers is None:
- registers = defaultdict(int)
- to_send = []
- # last_sound_played = None
- stuck = True
- while cur_instr_ind >= 0 and cur_instr_ind < len(instructions):
- # print 'registers', registers
- cur_instr = instructions[cur_instr_ind]
- # print 'prog', prog_id, 'executing', cur_instr
- if cur_instr[0] == 'snd':
- intified = intify(registers, cur_instr[1])
- to_send.append(intified)
- # last_sound_played = intified
- elif cur_instr[0] == 'rcv':
- if msg_queue:
- registers[cur_instr[1]] = msg_queue.pop(0)
- else:
- return cur_instr_ind, to_send, stuck
- # if registers[cur_instr[1]] != 0:
- # return last_sound_played
- elif cur_instr[0] == 'jgz':
- intified = intify(registers, cur_instr[2])
- if registers[cur_instr[1]] > 0:
- stuck = False
- cur_instr_ind += intified
- continue
- else:
- intified = intify(registers, cur_instr[2])
- if cur_instr[0] == 'set':
- registers[cur_instr[1]] = intified
- elif cur_instr[0] == 'add':
- registers[cur_instr[1]] += intified
- elif cur_instr[0] == 'mul':
- registers[cur_instr[1]] *= intified
- elif cur_instr[0] == 'mod':
- registers[cur_instr[1]] = registers[cur_instr[1]] % intified
- stuck = False
- cur_instr_ind += 1
- return None, to_send, True
- def exec_programs(instructions, num_programs=2):
- all_regs = [defaultdict(int) for _ in range(num_programs)]
- all_regs[0]['p'] = 0
- all_regs[1]['p'] = 1
- deadlocked_time = [0 for _ in range(num_programs)]
- instr_inds = [0 for _ in range(num_programs)]
- msg_queues = [[] for _ in range(num_programs)]
- prog_one_sent = 0
- # while False in deadlocked:
- while any([t < 1 for t in deadlocked_time]):
- print '\ndeadlocked_time', deadlocked_time
- print 'instr_inds', instr_inds
- print 'registers 0', all_regs[0]
- print 'registers 1', all_regs[1]
- # print 'msg queues 0', msg_queues[0]
- # print 'msg queues 1', msg_queues[1]
- for i in range(num_programs):
- dest_ind = 0 if i == 1 else 1
- if instr_inds[i] is None:
- deadlocked[i] = True
- continue
- new_instr_ind, to_send, cur_deadlocked = exec_instructions(instructions, all_regs[i], instr_inds[i], msg_queues[i])
- if to_send:
- # print 'prog', i, 'sending', to_send
- if i == 1:
- prog_one_sent += len(to_send)
- msg_queues[dest_ind] += to_send
- # if i == 1:
- # prog_one_sent += 1
- # msg_queues[dest_ind].append(to_send)
- if not cur_deadlocked:
- deadlocked_time[i] = 0
- else:
- deadlocked_time[i] += 1
- if new_instr_ind is not None:
- instr_inds[i] = new_instr_ind
- print '\nfinal deadlocked time', deadlocked_time
- return prog_one_sent
- instructions = []
- with open('q18input.txt', 'r') as f:
- for line in f:
- instructions.append(line.strip().split())
- # print exec_instructions(instructions)
- # assert exec_instructions(instructions) == 2951
- print exec_programs(instructions)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement