def main2(): program = open_program('input') program[0] = 2 computer = IntCode(program, default_memory=8000) halted = False grid = defaultdict(lambda: 0) while not halted: halted = computer.run() while computer.has_output(): x_pos = computer.get_output() y_pos = computer.get_output() tile_id = computer.get_output() if tile_id == 3: paddle_x = x_pos elif tile_id == 4: ball_x = x_pos grid[Point(x_pos, y_pos)] = tile_id print_grid(grid) if paddle_x > ball_x: user_input = -1 elif paddle_x < ball_x: user_input = 1 else: user_input = 0 computer.add_input(user_input) return grid[Point(-1, 0)]
def check_point(program: Tuple[int], point: Point): computer = IntCode(list(program)) computer.add_input(point.x) computer.add_input(point.y) computer.run() output = computer.get_output() return output
def test_jump_immediate(self): ic = IntCode("3,3,1105,-1,9,1101,0,0,12,4,12,99,1") ic.add_input(0) ic.run() self.assertEqual(0, ic.output[0]) ic.add_input(1) ic.reset() ic.run() self.assertEqual(1, ic.output[0])
def test_jump_position(self): ic = IntCode("3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9") ic.add_input(0) ic.run() self.assertEqual(0, ic.output[0]) ic.add_input(1) ic.reset() ic.run() self.assertEqual(1, ic.output[0])
def test_less_than_8_immediate(self): ic = IntCode("3,3,1107,-1,8,3,4,3,99") ic.add_input(7) ic.run() self.assertEqual(1, ic.output[0]) ic.add_input(8) ic.reset() ic.run() self.assertEqual(0, ic.output[0])
def test_less_than_8_position(self): ic = IntCode("3,9,7,9,10,9,4,9,99,-1,8") ic.add_input(7) ic.run() self.assertEqual(1, ic.output[0]) ic.add_input(8) ic.reset() ic.run() self.assertEqual(0, ic.output[0])
def get_thruster_signal( ic: IntCode, amplifier_inputs: Tuple[int, int, int, int, int], input_value: int = 0, ): amplifiers: List[IntCode] = [] for i in amplifier_inputs: amp = IntCode(ic.code) amp.add_input(i) amplifiers.append(amp) while False in [a.halted for a in amplifiers]: for amp in amplifiers: amp.add_input(input_value) # amp.add_input(amplifier_inputs[i], input_value) amp.run(pause_on_output=True) input_value = amp.output[0] return input_value
def main(): text_input = get_raw_input() int_code = IntCode(text_input) int_code._int_code[0] = 2 tiles = dict() score = 0 paddle = Paddle() ball = Ball() while not int_code.finished: for co in tiles.keys(): if tiles[co] == Tiles.PADDLE: paddle.x = co[0] elif tiles[co] == Tiles.BALL: ball.set_x_pos(co[0]) ball.update_direction() if ball.direction == Directions.LEFT and paddle.x > ball.x: int_code.add_input(Joystick.LEFT) elif ball.direction == Directions.RIGHT and ball.x > paddle.x: int_code.add_input(Joystick.RIGHT) elif paddle.x > ball.x: int_code.add_input(Joystick.LEFT) elif paddle.x < ball.x: int_code.add_input(Joystick.RIGHT) else: int_code.add_input(Joystick.NEUTRAL) int_code.execute_till_halt() output_instructions = list(int_code.output_buffer.queue) i = 0 while i + 2 < len(output_instructions): x, y, id_ = output_instructions[i:i + 3] if x == -1 and y == 0: score = id_ else: tiles[(x, y)] = id_ i += 3 print_tiles(tiles) print('Score after all blocks are broken:', score)
def part1(): program = open_program('input') # computer = IntCode(program) count = 0 for y in range(50): for x in range(50): computer = IntCode(program[:]) computer.add_input(x) computer.add_input(y) computer.run() output = computer.get_output() if output: count += 1 else: pass # print(x,y) return count
def main(initial_color=0, filename='input'): program = open_program(filename) computer = IntCode(program) halted = False grid = defaultdict(lambda: 0) location = Point(0, 0) grid[location] = initial_color facing = Direction.UP while not halted: computer.add_input(grid[location]) halted = computer.run() color = computer.get_output() direction = computer.get_output() grid[location] = color location, facing = move_robot(location, facing, direction) print_grid(grid) return len(grid.keys())
def test_equality(self): ic = IntCode(""" 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""") ic.add_input(7) ic.run() self.assertEqual(999, ic.output[0]) ic.add_input(8) ic.reset() ic.run() self.assertEqual(1000, ic.output[0]) ic.add_input(9) ic.reset() ic.run() self.assertEqual(1001, ic.output[0])
class ASCII: """ Aft Scaffolding Control and Information Interface """ def __init__(self, software): self.program = IntCode(software) self.map = [] self.mapstring = "" self.intersections = [] def build_map(self): """ Builds a map from the ASCII output of the IntCode program. Assigns results to a map of characters as well as a single string. """ output = self.program.run() while output[-1] == 10: output.pop() output.reverse() self.map.append([]) maprow = 0 while len(output) > 0: char = chr(output.pop()) self.mapstring += char if char == "\n": self.map.append([]) maprow += 1 else: self.map[maprow].append(char) def _is_intersection(self, xpos, ypos): """ Check the neighbors of the XY position and return the number of neighbors that are a scaffold space. """ results = 0 for xshift, yshift in [(-1, 0), (1, 0), (0, 1), (0, -1)]: if self.map[ypos + yshift][xpos + xshift] in ["#", "<", ">", "^", "v"]: results += 1 if results == 4: return True else: return False def _get_intersections(self): """ Detect all the intersections on the map and populate the list with tuples of the XY intersections. """ for ypos in range(1, len(self.map) - 1): for xpos in range(1, len(self.map[0]) - 1): if self.map[ypos][xpos] == "#": if self._is_intersection(xpos, ypos): self.intersections.append((xpos, ypos)) def align(self): """ Return the alignment parameter sum. """ self._get_intersections() print(self.mapstring) sum = 0 for x, y in self.intersections: sum += x * y return sum def notify(self, routine): """ Primes the robot to perform the notification routine. Sets the initial opcode bit, primes the input, and prepares for the run. The run will return the amount of dust collected during the routine, which will be returned by the function. """ self.program.opcode[0] = 2 output = self.program.run() for line in routine: for char in line: self.program.add_input(ord(char)) self.program.add_input(10) self.program.add_input(ord("n")) self.program.add_input(10) output = self.program.run() result = output.pop() return result
def main(): text_input = get_raw_input() int_code = IntCode(text_input) int_code.add_input(1) int_code.execute()
def make_computer(program, index): computer = IntCode(program[:], default_memory=8000) computer.add_input(index) return computer
def part1(ic: IntCode): ic.add_input(1) ic.run() print(ic.output[0]) assert ic.output[0] == 5346030
def part2(ic: IntCode): ic.add_input(5) ic.reset() ic.run() print(ic.output[0]) assert ic.output[0] == 513116
def test_inputoutput(self): ic = IntCode("3,0,4,0,99") ic.add_input(8) ic.run() self.assertEqual(8, ic.output[0])
def part2(ic: IntCode): ic.add_input(2) ic.reset() ic.run() assert ic.output[0] == 83089
def part1(ic: IntCode): ic.add_input(1) ic.run() assert ic.output[0] == 3241900951