def get_area_painted(code): input_queue, output_queue = queue.Queue(), queue.Queue() machine = IntcodeMachine(input_queue, output_queue, code) processor = threading.Thread(target=machine.compute, daemon=True) current_coord = '{},{}'.format(robot['x'], robot['y']) cells[current_coord] = 0 processor.start() while processor.is_alive(): # provide input if needed, then get outputs output = output_queue.get() if output == IntcodeMachine.need_input: if current_coord in cells: input_queue.put(cells[current_coord]) else: input_queue.put(0) cell_color = output_queue.get() else: cell_color = output turn_direction = output_queue.get() # paint cells[current_coord] = cell_color # turn turn_robot(turn_direction) # move move_robot() current_coord = '{},{}'.format(robot['x'], robot['y']) processor.join() return len(cells)
def process_code(code, inputs): outputs = [] input_queue, output_queue = queue.Queue(), queue.Queue() machine = IntcodeMachine(input_queue, output_queue, code) processor = threading.Thread(target=machine.compute, daemon=True) processor.start() for value in inputs: input_queue.put(value) processor.join() while not output_queue.empty(): outputs.append(output_queue.get()) return outputs
def paint_code(code): input_queue, output_queue = queue.Queue(), queue.Queue() machine = IntcodeMachine(input_queue, output_queue, code) processor = threading.Thread(target=machine.compute, daemon=True) painting = [['.' for x in range(200)] for y in range(200)] robot['x'] = 100 robot['y'] = 100 current_coord = '{},{}'.format(robot['x'], robot['y']) cells[current_coord] = 1 processor.start() while processor.is_alive(): # provide input if needed, then get outputs output = output_queue.get() if output == IntcodeMachine.need_input: if current_coord in cells: input_queue.put(cells[current_coord]) else: input_queue.put(0) cell_color = output_queue.get() else: cell_color = output turn_direction = output_queue.get() # paint cells[current_coord] = cell_color # make drawing if cell_color == 0: painting[robot['y']][robot['x']] = '.' else: painting[robot['y']][robot['x']] = '#' # turn turn_robot(turn_direction) # move move_robot() current_coord = '{},{}'.format(robot['x'], robot['y']) processor.join() painting = [row for row in painting if '#' in ''.join(row)] for row in painting: print(re.sub(r'\.{20}', '', ''.join(row)).replace('.', ' '))
def run_code(code): input_queue, output_queue = queue.Queue(), queue.Queue() machine = IntcodeMachine(input_queue, output_queue, code) processor = threading.Thread(target=machine.compute, daemon=True) max_x = 0 max_y = 0 processor.start() while processor.is_alive(): x = output_queue.get() if x > max_x: max_x = x y = output_queue.get() if y > max_y: max_y = y tile = output_queue.get() tiles['{},{}'.format(x, y)] = tile processor.join() game_map = [[' ' for x in range(max_x + 1)] for y in range(max_y + 1)] blocks = 0 for tile in tiles: coords = tile.split(',') if tiles[tile] == 0: drawing = ' ' elif tiles[tile] == 1: drawing = 'X' elif tiles[tile] == 2: drawing = '#' blocks += 1 elif tiles[tile] == 3: drawing = '=' elif tiles[tile] == 4: drawing = 'O' game_map[int(coords[1])][int(coords[0])] = drawing for row in game_map: print(''.join(row)) print('\n{}'.format(blocks))
def test_load_program_from_file(self): computer = IntcodeMachine() computer.load_program_from_file('intcode_test_program.dat') self.assertEqual(computer.program, {k: v for k, v in enumerate(DEFAULT_PROGRAM)})
def setUp(self): self.computer = IntcodeMachine()
class TestIntcodeMachine(TestCase): def setUp(self): self.computer = IntcodeMachine() def test_addition(self): result = self.computer.run_program([1101, 100, -1, 4, 0]) self.assertEqual(result, 1101) def test_multiplication(self): result = self.computer.run_program([1002, 4, 3, 4, 33]) self.assertEqual(result, 1002) # Here's a larger example: # 3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31, # 1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104, # 999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99 # The above example program uses an input instruction to ask for a single number. # The program will then output 999 if the input value is below 8, output 1000 if the input value is equal to 8, # or output 1001 if the input value is greater than 8. def test_low_input(self): self.assertEqual(test_input_output(self.computer, 7), 999) def test_equal_input(self): self.assertEqual(test_input_output(self.computer, 8), 1000) def test_high_input(self): self.assertEqual(test_input_output(self.computer, 9), 1001) def test_relative_base(self): program = [ 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 ] input_queue = queue.Queue() output_queue = queue.Queue() self.computer.set_queues(input_queue, output_queue) result = self.computer.run_program(program) output_program = [] while not output_queue.empty(): output_program.append(output_queue.get()) self.assertEqual(program, output_program) def test_16bit_number(self): program = [1102, 34915192, 34915192, 7, 4, 7, 99, 0] input_queue = queue.Queue() output_queue = queue.Queue() self.computer.set_queues(input_queue, output_queue) result = self.computer.run_program(program) self.assertGreater(output_queue.get(), 2**16 - 1) def test_large_number(self): program = [104, 1125899906842624, 99] input_queue = queue.Queue() output_queue = queue.Queue() self.computer.set_queues(input_queue, output_queue) result = self.computer.run_program(program) self.assertEqual(output_queue.get(), 1125899906842624) def test_load_program_from_file(self): computer = IntcodeMachine() computer.load_program_from_file('intcode_test_program.dat') self.assertEqual(computer.program, {k: v for k, v in enumerate(DEFAULT_PROGRAM)})
import queue from intcode_machine import IntcodeMachine TEST_MODE = 1 SENSOR_BOOST_MODE = 2 input_queue = queue.Queue() output_queue = queue.Queue() computer = IntcodeMachine(input_queue, output_queue) computer.load_program_from_file('BOOST.dat') input_queue.put(SENSOR_BOOST_MODE) computer.run() while not output_queue.empty(): print(output_queue.get())