def run_trusters(code: List[str], phases: List[int]) -> int:
    output = 0
    while phases:
        phase = phases.pop()
        program = Program(code[:], input=[output, phase])
        run(program)
        output = program.output[0]
    return output
def run_thrusters_feedback(code: List[str], phases: List[int]) -> int:
    output = [0]
    halts = [False] * len(phases)
    programs = [Program(code[:], input=[phase]) for phase in phases]
    while not all(halts):
        for i, program in enumerate(programs):
            program.input = output + program.input
            _, halts[i] = run_until_output(program)
            if not halts[i]:
                output, program.output = program.output, []
    return output[0]
Ejemplo n.º 3
0
def run_robot(program: Program,
              main_routine: str,
              functions: List[str],
              camera_feed: bool = True) -> None:
    # Force robot to wakeup
    program[0] = 2
    # Supply the main movement routine:
    run(program)
    program.input.extend(to_ascii(main_routine, reverse=True))
    run(program)
    for f in functions:
        program.input.extend(to_ascii(f, reverse=True))
        run(program)
    if camera_feed:
        program.input.extend(to_ascii('y', reverse=True))
    else:
        program.input.extend(to_ascii('n', reverse=True))
    program.reset_output()
    # I'm getting a spurious newline at this point, so clear it out.
    run_until_matches(program, outseq=to_ascii(''))
    program.reset_output()

    halt = False
    while not halt:
        _, halt = run_until_matches(program, outseq=to_ascii('\n'))
        console = to_square(program.output)
        draw_array(console, conversion_table=CONVERSION_DICT)
        print()
        program.reset_output()
Ejemplo n.º 4
0
def explore(program: Program) -> Map:
    map: Map = {}
    position: Point = (0, 0)
    current_direction, output = NORTH, None
    # Move north until we hit a wall.
    while True:
        program.add_input(current_direction)
        _, _ = run_until_output(program)
        output = program.output.pop()
        if output == MOVE_SUCCESS:
            position = next_position(position, current_direction)
            map[position] = EMPTY
            continue
        elif output == HIT_WALL:
            map[next_position(position, current_direction)] = WALL
            current_direction = TURN_LEFT[current_direction]
            break
    # Now the main explore loop. The goal here is to make sure we keep a wall
    # to the right. We start with a wall to the north of us.
    have_moved = False
    while True:
        program.add_input(current_direction)
        _, _ = run_until_output(program)
        output = program.output.pop()
        if output == HIT_WALL:
            map[next_position(position, current_direction)] = WALL
            current_direction = TURN_LEFT[current_direction]
        elif output == FOUND_OXYGEN:
            position, have_moved = next_position(position,
                                                 current_direction), True
            map[position] = OXYGEN
        elif output == MOVE_SUCCESS:
            position, have_moved = next_position(position,
                                                 current_direction), True
            map[position] = EMPTY
            # Check if there is a wall to our right.
            current_direction = TURN_RIGHT[current_direction]
            program.add_input(current_direction)
            _, _ = run_until_output(program)
            output = program.output.pop()
            # If we hit a wall, there is still a wall to our right, so we
            # should keep going.
            if output == HIT_WALL:
                map[next_position(position, current_direction)] = WALL
                current_direction = TURN_LEFT[current_direction]
            # There is no longer a wall to the right, so we need to turn and
            # move in that direction.
            elif output == MOVE_SUCCESS:
                position = next_position(position, current_direction)
                map[position] = EMPTY
            elif output == FOUND_OXYGEN:
                position = next_position(position, current_direction)
                map[position] = OXYGEN
        # We're back where we started, so we can bail.
        if have_moved and position == (0, 0):
            break
    return map
Ejemplo n.º 5
0
from intcode.intcode import Program, run

TEST0 = [
    109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99
]
p = Program(TEST0[:])
run(p)
print(p.output)

TEST1 = [1102, 34915192, 34915192, 7, 4, 7, 99, 0]
p = Program(TEST1[:])
run(p)
print(p.output)

TEST2 = [104, 1125899906842624, 99]
p = Program(TEST2[:])
run(p)
print(p.output)

BOOST = [
    1102, 34463338, 34463338, 63, 1007, 63, 34463338, 63, 1005, 63, 53, 1102,
    1, 3, 1000, 109, 988, 209, 12, 9, 1000, 209, 6, 209, 3, 203, 0, 1008, 1000,
    1, 63, 1005, 63, 65, 1008, 1000, 2, 63, 1005, 63, 902, 1008, 1000, 0, 63,
    1005, 63, 58, 4, 25, 104, 0, 99, 4, 0, 104, 0, 99, 4, 17, 104, 0, 99, 0, 0,
    1102, 1, 37, 1007, 1102, 24, 1, 1006, 1102, 26, 1, 1012, 1101, 528, 0,
    1023, 1102, 256, 1, 1027, 1102, 466, 1, 1029, 1102, 1, 629, 1024, 1101, 0,
    620, 1025, 1101, 0, 0, 1020, 1102, 1, 30, 1004, 1101, 39, 0, 1003, 1102,
    36, 1, 1005, 1102, 531, 1, 1022, 1102, 32, 1, 1019, 1101, 0, 27, 1000,
    1101, 0, 28, 1016, 1101, 1, 0, 1021, 1101, 23, 0, 1013, 1102, 1, 25, 1015,
    1102, 1, 21, 1008, 1102, 1, 22, 1018, 1102, 1, 34, 1014, 1102, 475, 1,
    1028, 1101, 33, 0, 1002, 1101, 0, 35, 1011, 1102, 1, 20, 1009, 1102, 38, 1,
Ejemplo n.º 6
0
import sys
from collections import defaultdict
from queue import Queue
from intcode.intcode import Program
from aocd import data, submit

# Make sure AOC_SESSION is updated! (Chrome inspector -> Application tab -> session)

#lines = data.strip().split('\n')
prog = Program(data)

# submit(?, part="a", day=5, year=2019)
# submit(?, part="b", day=5, year=2019)
#prog.log_info()
#prog.show(0)

prog.run(input=[5])
Ejemplo n.º 7
0
    def get_ball_position(self) -> Optional[Tuple[int, int]]:
        return self.get_position(needle=4)

    def draw_screen(self) -> None:
        if not self.screen:
            return
        minx = min(k[0] for k in self.screen)
        maxx = max(k[0] for k in self.screen)
        miny = min(k[1] for k in self.screen)
        maxy = max(k[1] for k in self.screen)
        area = np.zeros(shape=(maxx - minx + 1, maxy - miny + 1), dtype=int)
        for (x, y), id in self.screen.items():
            area[x - minx, y - miny] = id
        strrep = TILE_DRAW_ARRAY[area]
        for row in strrep.T:
            print(''.join(row))
        print(f"Score: {self.score}")
        print(f"Paddle: {self.paddle}")
        print(f"Ball: {self.ball}")


program = Program.from_file(open('./data/game.txt', 'r'))
game = Breakout(program)
game.run_game()

#screen = run_game(program)
#draw_screen(screen)

# n_blocks = sum(val == 2 for val in screen.values())
# print(f"There are {n_blocks} on screen at exit.")
Ejemplo n.º 8
0
             position: Optional[Point] = None,
             path: Optional[List[Point]] = None) -> None:
    if not map:
        return
    minx = min(k[0] for k in map)
    maxx = max(k[0] for k in map)
    miny = min(k[1] for k in map)
    maxy = max(k[1] for k in map)
    #area = np.zeros(shape=(maxx - minx + 1, maxy - miny + 1), dtype=int)
    area = np.zeros(shape=(50, 50), dtype=int)
    for (x, y), id in map.items():
        area[x - 25, y - 25] = id
    strrep = TILE_DRAW_ARRAY[area]
    if position:
        strrep[(position[0] - 25, position[1] - 25)] = '@'
    if path:
        for p in path:
            strrep[p[0] - 25, p[1] - 25] = '.'
    for row in strrep.T:
        print(''.join(row))


program = Program.from_file(open('./data/program.txt'))
map = explore(program)
path = find_shortest_path(map, (0, 0))

# draw_map(map, (0, 0), path[1:])
# print(f"The shortest path to the oxygen is {len(path)} steps.")

i = spawn_gas(map, get_oxygen_position(map))
print(f"It takes {i} turns to fill the ship with oxygen.")
Ejemplo n.º 9
0
        program.input.extend(to_ascii('n', reverse=True))
    program.reset_output()
    # I'm getting a spurious newline at this point, so clear it out.
    run_until_matches(program, outseq=to_ascii(''))
    program.reset_output()

    halt = False
    while not halt:
        _, halt = run_until_matches(program, outseq=to_ascii('\n'))
        console = to_square(program.output)
        draw_array(console, conversion_table=CONVERSION_DICT)
        print()
        program.reset_output()


def to_ascii(code: str, reverse: bool = False) -> List[int]:
    if reverse:
        return ([ord(ch) for ch in code] + [LINE_END])[::-1]
    else:
        return [ord(ch) for ch in code] + [LINE_END]


program = Program.from_file(open('./data/ascii.txt', 'r'))

#run(program)
#console_out = to_square(program.output)
#draw_array(console_out, conversion_table=CONVERSION_DICT)
# total_alignment_parameter = sum(x * y for x, y in iter_intersections(console_out))
# print(f"The total allignment parameter is {total_alignment_parameter}")

run_robot(program, 'A,A,B,A', ['R,2', '2', '1'])
Ejemplo n.º 10
0
    294, 0, 0, 105, 1, 0, 1105, 1, 99999, 1106, 0, 300, 1105, 1, 99999, 1, 225,
    225, 225, 1101, 314, 0, 0, 106, 0, 0, 1105, 1, 99999, 1107, 677, 677, 224,
    1002, 223, 2, 223, 1005, 224, 329, 101, 1, 223, 223, 7, 677, 677, 224,
    1002, 223, 2, 223, 1005, 224, 344, 1001, 223, 1, 223, 7, 226, 677, 224,
    102, 2, 223, 223, 1006, 224, 359, 101, 1, 223, 223, 1108, 226, 226, 224,
    1002, 223, 2, 223, 1005, 224, 374, 1001, 223, 1, 223, 8, 226, 677, 224,
    1002, 223, 2, 223, 1006, 224, 389, 1001, 223, 1, 223, 1108, 226, 677, 224,
    1002, 223, 2, 223, 1006, 224, 404, 101, 1, 223, 223, 1107, 677, 226, 224,
    102, 2, 223, 223, 1006, 224, 419, 101, 1, 223, 223, 1007, 677, 677, 224,
    1002, 223, 2, 223, 1005, 224, 434, 101, 1, 223, 223, 7, 677, 226, 224, 102,
    2, 223, 223, 1006, 224, 449, 1001, 223, 1, 223, 1008, 226, 677, 224, 1002,
    223, 2, 223, 1006, 224, 464, 101, 1, 223, 223, 8, 677, 677, 224, 1002, 223,
    2, 223, 1006, 224, 479, 101, 1, 223, 223, 108, 226, 226, 224, 102, 2, 223,
    223, 1005, 224, 494, 101, 1, 223, 223, 1107, 226, 677, 224, 1002, 223, 2,
    223, 1006, 224, 509, 101, 1, 223, 223, 107, 226, 226, 224, 1002, 223, 2,
    223, 1006, 224, 524, 1001, 223, 1, 223, 107, 677, 677, 224, 1002, 223, 2,
    223, 1005, 224, 539, 101, 1, 223, 223, 1007, 226, 226, 224, 102, 2, 223,
    223, 1006, 224, 554, 101, 1, 223, 223, 108, 677, 677, 224, 102, 2, 223,
    223, 1005, 224, 569, 101, 1, 223, 223, 107, 677, 226, 224, 102, 2, 223,
    223, 1005, 224, 584, 101, 1, 223, 223, 1008, 226, 226, 224, 102, 2, 223,
    223, 1006, 224, 599, 101, 1, 223, 223, 1108, 677, 226, 224, 1002, 223, 2,
    223, 1006, 224, 614, 101, 1, 223, 223, 8, 677, 226, 224, 102, 2, 223, 223,
    1005, 224, 629, 1001, 223, 1, 223, 1008, 677, 677, 224, 102, 2, 223, 223,
    1006, 224, 644, 101, 1, 223, 223, 1007, 226, 677, 224, 102, 2, 223, 223,
    1005, 224, 659, 101, 1, 223, 223, 108, 226, 677, 224, 102, 2, 223, 223,
    1006, 224, 674, 101, 1, 223, 223, 4, 223, 99, 226
]

program = Program(code=CODE[:], input=[1])
run(program)
print(program.output)