Ejemplo n.º 1
0
def run_bot(program, init_val):
    comp = IntComputer(program)
    panel = defaultdict(lambda: 0)
    botpos = (0, 0)
    botdir = 0
    color = comp.run_until_output(init_val)
    turn = comp.run_until_output()
    while color != -1 and turn != -1:
        panel[botpos] = color
        botdir = (botdir + turn * 2 - 1) % 4
        change = {0: (0, 1), 1: (1, 0), 2: (0, -1), 3: (-1, 0)}[botdir]
        botpos = (botpos[0] + change[0], botpos[1] + change[1])
        color = comp.run_until_output(panel[botpos])
        turn = comp.run_until_output()
    return panel
Ejemplo n.º 2
0
class Ship:
    def __init__(self, program):
        self.program = program
        self.grid = defaultdict(lambda: ' ', {(0, 0): '.'})
        self.botpos = (0, 0)
        self.o2pos = None
        self.comp = IntComputer(program)
        self.dd = {1: (-1, 0), 2: (1, 0), 3: (0, -1), 4: (0, 1)}

    def run_step(self, d):
        pos = self.botpos
        out = self.comp.run_until_output(d)
        delta = self.dd[d]
        np = (pos[0] + delta[0], pos[1] + delta[1])
        self.grid[np] = {0: '#', 1: '.', 2: '.'}[out]
        if out == 2:
            self.o2pos = np
        if out in [1, 2]:
            self.botpos = np
        return out

    def count_of_unknown(self):
        count = 0
        for k, v in self.grid.items():
            if v == '.':
                adj = [self.grid[a] for a in adjacent(k) if a in self.grid]
                if len(adj) < 4 or ' ' in adj:
                    count += 1
        return count

    def diplay_grid(self):
        m1, m3 = self.grid[self.botpos], self.grid[(0, 0)]
        m2 = self.grid[self.o2pos] if self.o2pos else None
        self.grid[self.botpos] = 'B'
        self.grid[(0, 0)] = '@'
        if self.o2pos:
            self.grid[self.o2pos] = 'O'
        display_dict_as_grid(self.grid)
        self.grid[self.botpos] = m1
        self.grid[(0, 0)] = m3
        if m2:
            self.grid[self.o2pos] = m2

    # def explore_randomly(self, howlong=1000):
    #     d = randint(1, 4)
    #     for _ in range(howlong):
    #         out = self.run_step(d)
    #         if out == 0:
    #             d = randint(1, 4)
    #         if out in [1, 2]:
    #             if randint(1, 5) == 1:
    #                 d = randint(1, 4)

    # def explore_2(self, howlong=1000):
    #     d = randint(1, 4)
    #     for _ in range(howlong):
    #         out = self.run_step(d)
    #         if out == 0:
    #             d = (d + randint(0, 1) * 2 - 2) % 4 + 1

    def explore_left(self, howlong=1000):
        d = randint(1, 4)
        for _ in range(howlong):
            out = self.run_step(d)
            if out == 0:
                d = right[d]
            else:
                d = left[d]