Guest User

Untitled

a guest
Dec 11th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. import sys
  2. import collections
  3.  
  4. # usage: python3 11.1.py program-file initial-square-color
  5.  
  6. # intCode computer (generator interface, yields outputs, gets inputs from list)
  7. def run(inp):
  8.     initmem = list(map(int, open(sys.argv[1]).read().split(',')))
  9.     mem = collections.defaultdict(lambda: 0, enumerate(initmem))
  10.     pc,base = 0, 0
  11.     while mem[pc] != 99:
  12.         opcode = mem[pc] % 100
  13.         modes = ("%05d" % mem[pc])[0:3]
  14.         o1 = base if modes[2] == '2' else 0
  15.         o2 = base if modes[1] == '2' else 0
  16.         o3 = base if modes[0] == '2' else 0
  17.         if opcode in (1,2,3,4,5,6,7,8,9):
  18.             op1 = mem[pc+1] if modes[2] == '1' else mem[o1+mem[pc+1]]
  19.         if opcode in (1,2,5,6,7,8):
  20.             op2 = mem[pc+2] if modes[1] == '1' else mem[o2+mem[pc+2]]
  21.         if opcode in (1,2): # add and mul
  22.             f = {1: int.__add__, 2: int.__mul__}[opcode]
  23.             mem[o3+mem[pc+3]] = f(op1,op2)
  24.             pc += 4
  25.         elif opcode == 3: # input
  26.             mem[o1+mem[pc+1]] = inp.pop(0)
  27.             pc += 2
  28.         elif opcode  == 4: # output
  29.             pc += 2
  30.             yield op1
  31.         elif opcode == 5: # jump if true
  32.             pc = op2 if op1 != 0 else pc+3
  33.         elif opcode == 6: # jump if false
  34.             pc = op2 if op1 == 0 else pc+3
  35.         elif opcode == 7: # less than
  36.             mem[o3+mem[pc+3]] = 1 if op1 < op2 else 0
  37.             pc += 4
  38.         elif opcode == 8: # equals
  39.             mem[o3+mem[pc+3]] = 1 if op1 == op2 else 0
  40.             pc += 4
  41.         elif opcode == 9: # set relative base
  42.             base += op1
  43.             pc += 2
  44.         else: raise Exception("invalid opcode %d" % opcode)
  45.  
  46. # painting robot, supports infinite grid as a dict for (x,y):color
  47. pipe = list()
  48. robot = run(pipe)
  49. hull = collections.defaultdict(lambda: 0)
  50. hull[(0,0)] = int(sys.argv[2])
  51. d, x, y = 0, 0, 0
  52. dirs = ((0,-1), (1,0), (0,1), (-1,0))
  53. try:
  54.     while True:
  55.         pipe.append(hull[(x,y)])
  56.         color = next(robot)
  57.         turn = next(robot)
  58.         hull[(x,y)] = color
  59.         d = (d+1)%4 if turn == 1 else (d-1)%4
  60.         dx, dy = dirs[d]
  61.         x, y = x+dx, y+dy
  62. except StopIteration:
  63.     pass
  64.  
  65. # answer part 1
  66. print(len(hull.keys()))
  67.  
  68. # convert the sparse dict into a grid and print
  69. minx = min(hull.keys(), key=lambda x: x[0])[0]
  70. maxx = max(hull.keys(), key=lambda x: x[0])[0]
  71. miny = min(hull.keys(), key=lambda x: x[1])[1]
  72. maxy = max(hull.keys(), key=lambda x: x[1])[1]
  73. grid = [[' ']*(maxx-minx+1) for _ in range(maxy-miny+1)]
  74. for (x,y),c in hull.items():
  75.     if c == 1:
  76.         grid[y-miny][x-minx] = '#'
  77. for line in grid:
  78.     print(''.join(line))
Advertisement
Add Comment
Please, Sign In to add comment