Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.96 KB | None | 0 0
  1. def getval(param, mem, index, jump, base):
  2.     if param == 0:
  3.         return mem[mem[index+jump]]
  4.     elif param == 1:
  5.         return mem[index+jump]
  6.     elif param == 2:
  7.         return mem[mem[index+jump]+base]
  8.     else:
  9.         print('invalid parameter value in getval: 0'.format(param))
  10.         exit()
  11.  
  12. def getdest(param, mem, index, jump, base):
  13.     if param == 0:
  14.         return mem[index+jump]
  15.     elif param == 1:
  16.         return index + jump
  17.     elif param == 2:
  18.         return mem[index+jump] + base
  19.     else:
  20.         print('invalid parameter value in getdest: {0}'.format(param))
  21.         exit()
  22.  
  23. def computer(memory, inputs):
  24.     mem = memory[0]
  25.     mem +=  [0]*100000
  26.     ind = memory[1]
  27.     base = memory[2]
  28.     outputs = list()
  29.    
  30.     while True:
  31.         instruction = (str(mem[ind])).zfill(5)
  32.         param3 = int(instruction[0])
  33.         param2 = int(instruction[1])
  34.         param1 = int(instruction[2])
  35.         opcode = int(instruction[3:])
  36.         if opcode == 1:
  37.             mem[getdest(param3, mem, ind, 3, base)] = getval(param1, mem, ind, 1, base) + getval(param2, mem, ind, 2, base)
  38.             ind += 4
  39.         elif opcode == 2:
  40.             mem[getdest(param3, mem, ind, 3, base)] = getval(param1, mem, ind, 1, base) * getval(param2, mem, ind, 2, base)
  41.             ind += 4
  42.         elif opcode == 3:
  43.             if len(inputs):
  44.                 mem[getdest(param1, mem, ind, 1, base)] = inputs.pop(0)
  45.                 ind += 2
  46.             else:
  47.                 memory[1] = ind
  48.                 return outputs+[ind]+[base]
  49.         elif opcode == 4:
  50.             outputs.append(getval(param1, mem, ind, 1, base))
  51.             ind += 2
  52.         elif opcode == 5:
  53.             ind = getval(param2, mem, ind, 2, base) if getval(param1, mem, ind, 1, base) else (ind + 3)
  54.         elif opcode == 6:
  55.             ind = getval(param2, mem, ind, 2, base) if not getval(param1, mem, ind, 1, base) else (ind + 3)
  56.         elif opcode == 7:
  57.             mem[getdest(param3, mem, ind, 3, base)] = 1 if getval(param1, mem, ind, 1, base) < getval(param2, mem, ind, 2, base) else 0
  58.             ind += 4
  59.         elif opcode == 8:
  60.             mem[getdest(param3, mem, ind, 3, base)] = 1 if getval(param1, mem, ind, 1, base) == getval(param2, mem, ind, 2, base) else 0
  61.             ind += 4
  62.         elif opcode == 9:
  63.             base += getval(param1, mem, ind, 1, base)
  64.             ind += 2
  65.         elif opcode == 99:
  66.             outputs.append('halt')
  67.             return outputs
  68.         else:
  69.             print("invalid opcode: {0}".format(opcode))
  70.             exit()
  71.            
  72. with open('inputs.txt') as f:
  73.     memory = list(map(int, f.readline().split(',')))
  74.  
  75. # 1 if #, 0 if .
  76. # if output 0, turn left 90, if output 1, turn right 90
  77. dirs = ['U', 'L', 'D', 'R']
  78. ind_dir = 0
  79. dict_dirs = {'U':(0, -1), 'D':(0,1), 'L':(-1,0), 'R':(1,0)}
  80. mapd = dict()
  81.  
  82. pos = (0,0)
  83. output = computer([memory, 0, 0], [1])
  84. mapd[pos] = '#' if output[0] == 1 else '.'
  85.  
  86. ind_dir = (ind_dir + 1)%4 if output[1] == 0 else (ind_dir - 1)%4
  87. new_dir = dirs[ind_dir]
  88. pos = (pos[0] + dict_dirs[new_dir][0], pos[1] + dict_dirs[new_dir][1])
  89.  
  90. if pos in mapd.keys():
  91.     color = mapd[pos]
  92. else:
  93.     color = '.'
  94. inp = 0 if color == '.' else 1
  95. # print(output)
  96.  
  97. while 'halt' not in output:
  98.     output = computer([memory, output[2], output[3]], [inp])
  99.     mapd[pos] = '#' if output[0] == 1 else '.'
  100.  
  101.     ind_dir = (ind_dir + 1)%4 if output[1] == 0 else (ind_dir - 1)%4
  102.     new_dir = dirs[ind_dir]
  103.     pos = (pos[0] + dict_dirs[new_dir][0], pos[1] + dict_dirs[new_dir][1])
  104.  
  105.     if pos in mapd.keys():
  106.         color = mapd[pos]
  107.     else:
  108.         color = '.'
  109.     inp = 0 if color == '.' else 1
  110.    
  111. h = max([x[1] for x in mapd.keys()])+1
  112. w = max([x[0] for x in mapd.keys()])+1
  113. # print(h,w)
  114.  
  115. grid =  [['.' for row in range(w)] for col in range(h)]
  116.  
  117. for k in mapd.keys():
  118.     grid[k[1]][k[0]] = mapd[k]
  119.            
  120. for line in grid:
  121.     print(''.join(line).replace('.',' '))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement