Advertisement
Guest User

AoC 2019 day 11

a guest
Dec 11th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. from intcode import IntComp
  2. import collections
  3. import concurrent.futures
  4.  
  5. def part1(input_text):
  6.     program = input_text[0]
  7.  
  8.     computer = IntComp(program)
  9.    
  10.     def run_robot(computer):
  11.         panel = collections.defaultdict(int)
  12.         pos = (0,0)
  13.         direction = (0,1)
  14.  
  15.         while not computer.halted:
  16.             computer.append_inputs(panel[pos])
  17.             computer.waiting_on_input.wait()
  18.             color, turn = computer.outputs[-2:]
  19.             panel[pos] = color
  20.             direction = (direction[1], -direction[0]) if turn else (-direction[1], direction[0])
  21.             pos = (pos[0] + direction[0], pos[1] + direction[1])
  22.        
  23.         return len(panel)
  24.  
  25.     with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
  26.         executor.submit(computer.execute)
  27.         return executor.submit(run_robot, computer).result()
  28.    
  29.  
  30. def part2(input_text):
  31.     program = input_text[0]
  32.  
  33.     computer = IntComp(program)
  34.    
  35.     def run_robot(computer):
  36.         panel = collections.defaultdict(int)
  37.         pos = (0,0)
  38.         direction = (0,1)
  39.         panel[pos] = 1
  40.  
  41.         while not computer.halted:
  42.             computer.append_inputs(panel[pos])
  43.             computer.waiting_on_input.wait()
  44.             color, turn = computer.outputs[-2:]
  45.             panel[pos] = color
  46.             direction = (direction[1], -direction[0]) if turn else (-direction[1], direction[0])
  47.             pos = (pos[0] + direction[0], pos[1] + direction[1])
  48.        
  49.         return panel
  50.  
  51.     with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
  52.         executor.submit(computer.execute)
  53.         panel = executor.submit(run_robot, computer).result()
  54.    
  55.     min_x = 0
  56.     min_y = 0
  57.     max_x = 0
  58.     max_y = 0
  59.  
  60.     for pos in panel:
  61.         min_x = min(pos[0], min_x)
  62.         min_y = min(pos[1], min_y)
  63.         max_x = max(pos[0], max_x)
  64.         max_y = max(pos[1], max_y)
  65.    
  66.     reps = {0: '.', 1: '#'}
  67.     panel_string = [' ']
  68.     for y in range(max_y, min_y-1, -1):
  69.         line = [reps[panel[(x, y)]] for x in range(min_x, max_x+1)]
  70.         panel_string.append(''.join(line))
  71.  
  72.     return '\n'.join(panel_string)
  73.  
  74.  
  75. if __name__ == '__main__':
  76.     from pathlib import Path
  77.     with open(next(Path().glob('**/AoC2019_day11_input.txt'))) as f:
  78.         input_text = f.read().splitlines()
  79.  
  80.     print('part 1:', part1(input_text))
  81.     print('part 2:', part2(input_text))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement