Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. minx,maxx,miny,maxy = 0, 0, 0, 0
  54. try:
  55.     while True:
  56.         pipe.append(hull[(x,y)])
  57.         color = next(robot)
  58.         turn = next(robot)
  59.         hull[(x,y)] = color
  60.         d = (d+1)%4 if turn == 1 else (d-1)%4
  61.         dx, dy = dirs[d]
  62.         x, y = x+dx, y+dy
  63.         minx,maxx,miny,maxy = min(x,minx),max(x,maxx),min(y,miny),max(y,maxy)
  64. except StopIteration:
  65.     pass
  66.  
  67. # answer part 1
  68. print(len(hull.keys()))
  69.  
  70. # convert the sparse dict into a grid and print
  71. grid = [[' ']*(maxx-minx+1) for _ in range(maxy-miny+1)]
  72. for (x,y),c in hull.items():
  73.     if c == 1:
  74.         grid[y-miny][x-minx] = '#'
  75. for line in grid:
  76.     print(''.join(line))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement