Exemplo n.º 1
0
def run_robot_run(core):

    panel = {}
    pr = PaintingRobot()

    computer = IntCodeComputer()
    computer.load_core(core)
    computer.state = ProgramState.running
    while computer.state == ProgramState.running:
        computer.set_input_buffer([pr.camera(panel)])

        inst = [None, None]
        while len(computer.output_buffer) == 0:
            computer.step()
            if computer.state != ProgramState.running:
                return len(pr._painted_panels)

        inst[0] = computer.output_buffer.pop()

        while len(computer.output_buffer) == 0:
            computer.step()
        inst[1] = computer.output_buffer.pop()

        pr.paint(inst[0], panel)
        pr.turn(inst[1])
Exemplo n.º 2
0
def main():
    with open("015.1.input.txt", "r") as f:
        core = [int(c) for c in f.readline().strip().split(",")]

    visited = {(0, 0): 3}
    computer = IntCodeComputer()
    computer.load_core(core)
    computer.state = ProgramState.running
    search_q = deque([Droid(computer, loc=(0, 0), depth=0)])

    while search_q:
        droid = search_q.popleft()
        for move in all_moves:
            res, new_droid = droid.clone_and_move(move)
            if res == 0:
                visited[new_droid.loc] = 1
                continue
            elif res == 1:
                if new_droid.loc in visited:
                    continue
                else:
                    search_q.append(new_droid)
                    visited[new_droid.loc] = 2
            elif res == 2:
                visited[new_droid.loc] = 4
                print(new_droid.depth)
                return 0

        display_visited(visited)
        print(droid.depth)
        time.sleep(.1)
Exemplo n.º 3
0
class ArcadeBox:
    def __init__(self, core, debug=False):
        self.game = Game()
        self.computer = IntCodeComputer(debug=debug)
        self.computer.load_core(core)
        self.computer.state = ProgramState.running

    def play_step(self, joy):
        self.computer.input_buffer = [joy]
        while len(self.computer.input_buffer) and self.computer.state == ProgramState.running:
            self.computer.step()
            if len(self.computer.output_buffer) == 3:
                x, y, t = self.computer.output_buffer
                self.computer.output_buffer.clear()
                self.game.set_state(x, y, t)

    def display(self):
        self.game.draw()
Exemplo n.º 4
0
def main():
    with open(sys.argv[1], "r") as f:
        core = [int(c) for c in f.readline().strip().split(",")]

    screen = {}

    computer = IntCodeComputer()
    computer.load_core(core)
    computer.state = ProgramState.running
    while computer.state == ProgramState.running:
        computer.step()

        if len(computer.output_buffer) == 3:
            x, y, t = computer.output_buffer
            computer.output_buffer.clear()
            screen[(x, y)] = t

    print(len([s for s in screen.values() if s == 2]))
    print_screen(screen)
Exemplo n.º 5
0
def create_map():
    with open("015.1.input.txt", "r") as f:
        core = [int(c) for c in f.readline().strip().split(",")]

    # 1 = block
    # 2 = corridor

    visited = {(0, 0): 2}
    computer = IntCodeComputer()
    computer.load_core(core)
    computer.state = ProgramState.running
    search_q = deque([Droid(computer, loc=(0, 0), depth=0)])

    oxy_source = None

    while search_q:
        droid = search_q.popleft()
        for move in all_moves:
            loc2 = new_loc(droid.loc, move)
            if loc2 in visited:
                continue

            res, new_droid = droid.clone_and_move(move)
            if res == 0:
                visited[new_droid.loc] = 1
                continue
            else:
                search_q.append(new_droid)
                visited[new_droid.loc] = 2
                if res == 2:
                    oxy_source = new_droid.loc

        display_visited(visited)
        print(f"Mapping: depth={droid.depth} clones={len(search_q)}")
        time.sleep(.01)

    return visited, oxy_source
class Droid:
    def __init__(self, core, loc):
        self.computer = IntCodeComputer()
        self.computer.load_core(core)
        self.computer.state = ProgramState.running
        self.loc = loc
        self.heading = 4
        self.grid_map = {self.loc: 1}
        # self.headings = list(next_heading[self.heading])

    def move(self):
        self.heading = self.get_next_heading()
        self.computer.set_input_buffer([self.heading])
        while len(self.computer.output_buffer) == 0:
            self.computer.step()
        res = self.computer.output_buffer.pop()

        if res != 0:
            self.loc = new_loc(self.loc, self.heading)
            if res == 2:
                self.grid_map[self.loc] = -2  # Oxygen
            else:
                self.grid_map[self.loc] = self.grid_map.get(self.loc,
                                                            0) + 1  # visited
        else:
            self.grid_map[new_loc(self.loc, self.heading)] = -1  # block

        return res

    def get_next_heading(self):
        new_heading = self.heading

        # Find an uncharted way
        for n in [0, 1, 2, 3]:
            _loc = new_loc(self.loc, new_heading)
            if _loc not in self.grid_map:
                return new_heading
            else:
                new_heading = turn_right[new_heading]

        # Find a free way that we have traveled least
        new_heading = self.heading
        min_trips, min_heading = 10000, new_heading
        for n in [0, 1, 2, 3]:
            _loc = new_loc(self.loc, new_heading)
            if self.grid_map[_loc] != -1:
                if self.grid_map[_loc] < min_trips:
                    min_trips = self.grid_map[_loc]
                    min_heading = new_heading
            new_heading = turn_right[new_heading]

        return min_heading

    def display_map(self):
        legend = {-2: "$", -1: "#", 1: ".", 0: " "}
        extent = [-20, -30, 30, 30]
        print(chr(27) + "[2J")
        print(f"h={self.heading}")
        for col in range(extent[0], extent[2]):
            print("".join(
                "*" if (row, col) ==
                self.loc else legend[min(self.grid_map.get((row, col), 0), 1)]
                for row in range(extent[1], extent[3])))
        time.sleep(.1)