def compute_play(interactive=True): if interactive: print("Press A to move left, D to move right") print("Press ENTER to start") input() optcode = read_input() interpreter = OptcodeInterpreter(optcode, quiet_mode=True) interpreter.memory[0] = 2 arcade_screen = ArcadeScreen() player = Player(arcade_screen) def on_input_requested(): while not interpreter.output.empty(): draw_instructions(interpreter, arcade_screen) if interactive: arcade_screen.render() int_input = get_input() else: int_input = player.act() interpreter.input.put(int_input) interpreter.set_input_request_listener(on_input_requested) interpreter.run() while not interpreter.output.empty(): draw_instructions(interpreter, arcade_screen) if interactive: arcade_screen.render() return arcade_screen.get_score()
def compute(): space = SpaceMap() droid = RepairDroid() space.explore(droid.get_position(), POINT_EMPTY) optcode = read_input() interpreter = OptcodeInterpreter(optcode, quiet_mode=True) def on_input_requested(): if not interpreter.output.empty(): # Get status status = interpreter.output.get() requested_point = droid.get_requested_position() if status == 0: # Hit a wall space.explore(requested_point, POINT_WALL) elif status == 1: # Empty space, position updated space.explore(requested_point, POINT_EMPTY) droid.set_position(requested_point) elif status == 2: # Oxygen space, position updated space.explore(requested_point, POINT_OXYGEN) droid.set_oxigen_position(requested_point) droid.set_position(requested_point) print("Oxigen found at distance:", space.get_distance_from_start(requested_point)) move = droid.act(space) if move is None: compute_oxygen_time(space, droid.get_oxygen_position()) interpreter.input.put(0) else: interpreter.input.put(move) interpreter.set_input_request_listener(on_input_requested) interpreter.run()
def explore(): optcode = read_input() interpreter = OptcodeInterpreter(optcode, ascii_mode=True) def on_input_requested(): if interpreter.input.empty(): instructions = input("Input:") if instructions == "help": print(""" Possible commands are - Movement via north, south, east, or west - To take an item the droid sees in the environment, use the command take <name of item>. For example, if the droid reports seeing a red ball, you can pick it up with take red ball. - To drop an item the droid is carrying, use the command drop <name of item>. For example, if the droid is carrying a green ball, you can drop it with drop green ball. - To get a list of all of the items the droid is currently carrying, use the command inv (for "inventory"). Extra spaces or other characters aren't allowed - instructions must be provided precisely. """) on_input_requested() else: if not instructions.endswith("\n"): instructions += "\n" for el in instructions: interpreter.input.put(el) interpreter.set_input_request_listener(on_input_requested) interpreter.run()
def get_bound(position, direction): x, y = position keep = True prev_point = None prev_status = None boundary = None while keep: optcode = read_input() interpreter = OptcodeInterpreter(optcode, quiet_mode=True) interpreter.input.put(x) interpreter.input.put(y) interpreter.run() point_status = interpreter.output.get() # Check if we changed status if prev_point is not None and point_status != prev_status: # Found boundary keep = False if point_status == 1: boundary = y else: boundary = prev_point else: # Move x towards outside if point_status == 0: delta = -1 else: delta = 1 prev_point = y prev_status = point_status y = y + delta * direction return boundary
def alert_all_droids(): space = get_space_configuration() moves = get_all_moves(space) moves, sequence = split_moves(moves) a, b, c = moves # Run optcode = read_input() optcode[0] = 2 interpreter = OptcodeInterpreter(optcode, quiet_mode=True) # Input sequence sequence_str = convert_sequence(sequence) for el in sequence_str: interpreter.input.put(ord(el)) # Configure routines for el in convert_sequence(a): interpreter.input.put(ord(el)) for el in convert_sequence(b): interpreter.input.put(ord(el)) for el in convert_sequence(c): interpreter.input.put(ord(el)) interpreter.input.put(ord('n')) interpreter.input.put(ord('\n')) interpreter.run() last_val = None while not interpreter.output.empty(): last_val = interpreter.output.get() return last_val
def compute(): # Run the program with open("input", "r") as hand: optcode_raw = hand.read() optcode = list(map(lambda el: int(el), optcode_raw.split(","))) interpreter = OptcodeInterpreter(optcode) interpreter.input.put(2) interpreter.run()
def get_space_configuration(): space = Space() camera = Camera(space) optcode = read_input() interpreter = OptcodeInterpreter(optcode, quiet_mode=True) interpreter.run() while not interpreter.output.empty(): element = interpreter.output.get() camera.view(element) return space
def get_points_in_beam(): counter = 0 for x in range(50): for y in range(50): optcode = read_input() interpreter = OptcodeInterpreter(optcode, quiet_mode=True) interpreter.input.put(x) interpreter.input.put(y) interpreter.run() res = interpreter.output.get() counter += res return counter
def get_hull_damage(extended_mode=False): optcode = read_input() interpreter = OptcodeInterpreter(optcode, ascii_mode=True, quiet_mode=True) if not extended_mode: springscript = [ "NOT A J", "NOT B T", "AND D T", "OR T J", "NOT C T", "AND D T", "OR T J", "WALK" ] else: springscript = [ "NOT H T", "OR E T", "AND F T", "NOT E J", "AND J T", "OR C T", "NOT T J", "NOT C J", "AND C J", "OR B J", "OR E J", "AND T J", "AND A J", "NOT J J", "AND D J", "RUN" ] ascii_instructions = "\n".join(springscript) + "\n" for el in ascii_instructions: interpreter.input.put(el) interpreter.run() return interpreter.output.get()
def test3(): print("Test 3: Should print large number in the middle") optcode_raw = "104,1125899906842624,99" optcode = list(map(lambda el: int(el), optcode_raw.split(","))) interpreter = OptcodeInterpreter(optcode) interpreter.run()
def test2(): print("Test 2: Should print 16 bit number") optcode_raw = "1102,34915192,34915192,7,4,7,99,0" optcode = list(map(lambda el: int(el), optcode_raw.split(","))) interpreter = OptcodeInterpreter(optcode) interpreter.run()
def test(): print("Test 1: Should print itself") optcode_raw = "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99" optcode = list(map(lambda el: int(el), optcode_raw.split(","))) interpreter = OptcodeInterpreter(optcode) interpreter.run()