def solve_gen(m: 'Model'): yield Cmd.Flip() pos = None for x in snake_fill_gen(m, Pos(0, 0, 0), Pos(m.R - 1, m.R - 1, m.R - 1)): if type(x) == Pos: pos = x else: yield x for x in return_home_gen(pos, Pos(0, 0, 0)): yield x yield Cmd.Flip() yield Cmd.Halt()
def cmd_from_cpp(ccmd): if isinstance(ccmd, Cpp.Halt): return commands.Halt() if isinstance(ccmd, Cpp.Wait): return commands.Wait() if isinstance(ccmd, Cpp.Flip): return commands.Flip() if isinstance(ccmd, Cpp.SMove): return commands.SMove(ccmd.lld) if isinstance(ccmd, Cpp.LMove): return commands.LMove(ccmd.sld1, ccmd.sld2) if isinstance(ccmd, Cpp.Fission): return commands.Fission(ccmd.nd, ccmd.m) if isinstance(ccmd, Cpp.Fill): return commands.Fill(ccmd.nd) if isinstance(ccmd, Cpp.Void): return commands.Void(ccmd.nd) if isinstance(ccmd, Cpp.GFill): return commands.GFill(ccmd.nd, ccmd.fd) if isinstance(ccmd, Cpp.GVoid): return commands.GVoid(ccmd.nd, ccmd.fd) assert False, ccmd
def finish(self): for x in navigate(self.state.bots[0].pos, Pos(0, 0, 0)): yield x yield Cmd.Halt()
def solve_gen(m: 'Model', x_cnt: 'int', z_cnt: 'int', low): (bb_a, bb_b) = bounding_box(m) # For narrow models x_cnt = min(x_cnt, (bb_b.x - bb_a.x + 1) // 2) z_cnt = min(z_cnt, (bb_b.z - bb_a.z + 1) // 2) if not low: yield Cmd.Flip() zones = partition_space(bb_a, bb_b, x_cnt, z_cnt) # Spawn bots # # State is used to spawn bots and to provide initial coords and ids st = State.initial_state(39) yield from st.spawn_bots(x_cnt, z_cnt) bots = st.grid # Navigate solve, and go to safe position for merging tasks = {} for z in range(z_cnt): for x in range(x_cnt): id = bots[z][x] bot = st[bots[z][x]] (a, b) = zones[z][x] d = (x_cnt + z_cnt) - (bot.pos.x + bot.pos.y) tasks[id] = sequence(with_delay(d, agent_gen(m, bot.pos, a, b)), go_to_start(a, bb_b)) for x in merge_tasks(tasks): if type(x) == dict: pos = x else: yield x # # Merging # # Combine rows t = [] for i in range(len(bots)): t.append([x for x in merge_line(bots[i], pos, Diff(1, 0, 0))]) for i in range(len(t[0])): tasks = {} for j in range(len(bots)): tasks.update(t[j][i]) yield from merge_tasks(tasks) # Combine col col = [x[0] for x in bots] for t in merge_line(col, pos, Diff(0, 0, 1)): yield from merge_tasks(t) # Return home for x in navigate(pos[1], Pos(0, 0, 0)): yield x if not low: yield Cmd.Flip() yield Cmd.Halt()