Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def getval(param, mem, index, jump, base):
- if param == 0:
- return mem[mem[index+jump]]
- elif param == 1:
- return mem[index+jump]
- elif param == 2:
- return mem[mem[index+jump]+base]
- else:
- print('invalid parameter value in getval: 0'.format(param))
- exit()
- def getdest(param, mem, index, jump, base):
- if param == 0:
- return mem[index+jump]
- elif param == 1:
- return index + jump
- elif param == 2:
- return mem[index+jump] + base
- else:
- print('invalid parameter value in getdest: {0}'.format(param))
- exit()
- def computer(memory, inputs):
- mem = memory[0]
- mem += [0]*100000
- ind = memory[1]
- base = memory[2]
- outputs = list()
- while True:
- instruction = (str(mem[ind])).zfill(5)
- param3 = int(instruction[0])
- param2 = int(instruction[1])
- param1 = int(instruction[2])
- opcode = int(instruction[3:])
- if opcode == 1:
- mem[getdest(param3, mem, ind, 3, base)] = getval(param1, mem, ind, 1, base) + getval(param2, mem, ind, 2, base)
- ind += 4
- elif opcode == 2:
- mem[getdest(param3, mem, ind, 3, base)] = getval(param1, mem, ind, 1, base) * getval(param2, mem, ind, 2, base)
- ind += 4
- elif opcode == 3:
- if len(inputs):
- mem[getdest(param1, mem, ind, 1, base)] = inputs.pop(0)
- ind += 2
- else:
- memory[1] = ind
- return outputs+[ind]+[base]
- elif opcode == 4:
- outputs.append(getval(param1, mem, ind, 1, base))
- ind += 2
- elif opcode == 5:
- ind = getval(param2, mem, ind, 2, base) if getval(param1, mem, ind, 1, base) else (ind + 3)
- elif opcode == 6:
- ind = getval(param2, mem, ind, 2, base) if not getval(param1, mem, ind, 1, base) else (ind + 3)
- elif opcode == 7:
- mem[getdest(param3, mem, ind, 3, base)] = 1 if getval(param1, mem, ind, 1, base) < getval(param2, mem, ind, 2, base) else 0
- ind += 4
- elif opcode == 8:
- mem[getdest(param3, mem, ind, 3, base)] = 1 if getval(param1, mem, ind, 1, base) == getval(param2, mem, ind, 2, base) else 0
- ind += 4
- elif opcode == 9:
- base += getval(param1, mem, ind, 1, base)
- ind += 2
- elif opcode == 99:
- outputs.append('halt')
- return outputs
- else:
- print("invalid opcode: {0}".format(opcode))
- exit()
- with open('inputs.txt') as f:
- memory = list(map(int, f.readline().split(',')))
- # 1 if #, 0 if .
- # if output 0, turn left 90, if output 1, turn right 90
- dirs = ['U', 'L', 'D', 'R']
- ind_dir = 0
- dict_dirs = {'U':(0, -1), 'D':(0,1), 'L':(-1,0), 'R':(1,0)}
- mapd = dict()
- pos = (0,0)
- output = computer([memory, 0, 0], [1])
- mapd[pos] = '#' if output[0] == 1 else '.'
- ind_dir = (ind_dir + 1)%4 if output[1] == 0 else (ind_dir - 1)%4
- new_dir = dirs[ind_dir]
- pos = (pos[0] + dict_dirs[new_dir][0], pos[1] + dict_dirs[new_dir][1])
- if pos in mapd.keys():
- color = mapd[pos]
- else:
- color = '.'
- inp = 0 if color == '.' else 1
- # print(output)
- while 'halt' not in output:
- output = computer([memory, output[2], output[3]], [inp])
- mapd[pos] = '#' if output[0] == 1 else '.'
- ind_dir = (ind_dir + 1)%4 if output[1] == 0 else (ind_dir - 1)%4
- new_dir = dirs[ind_dir]
- pos = (pos[0] + dict_dirs[new_dir][0], pos[1] + dict_dirs[new_dir][1])
- if pos in mapd.keys():
- color = mapd[pos]
- else:
- color = '.'
- inp = 0 if color == '.' else 1
- h = max([x[1] for x in mapd.keys()])+1
- w = max([x[0] for x in mapd.keys()])+1
- # print(h,w)
- grid = [['.' for row in range(w)] for col in range(h)]
- for k in mapd.keys():
- grid[k[1]][k[0]] = mapd[k]
- for line in grid:
- print(''.join(line).replace('.',' '))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement