def print_all_tasks(): tasks = task.all_tasks() f = "{: >10}" * 7 i = 0 total_score = 0 for t in tasks: if i % 20 == 0: print() print( f.format("area", "x", "y", "max_score", "clones", "spawns", "teleports")) i += 1 gs = gamestate.State(t) gs.start() area = np.sum(gs.interior) x = gs.X y = gs.Y num_clones = len(gs.clone_on_ground) num_spawns = len(gs.spawn_list) num_teles = len(gs.tele_on_ground) max_score = 1 + int(1000 * math.log(x * y) / math.log(2)) total_score += max_score print( f.format(area, x, y, max_score, num_clones, num_spawns, num_teles) + " " + os.path.basename(t.filename)) print() print("Total available score: ", total_score)
def prob1(t): assert os.path.basename(t.filename) == "prob-001.desc" gs = gamestate.State(t) w = gs.start() w.move_up() w.move_up() w.move_down() w.move_right() w.move_right() w.move_right() w.move_right() w.move_right() w.move_right() return gs
def print_tasks_in_directory(path): tasks = task.tasks_in_directory(path) for t in tasks: gs = gamestate.State(t) i = gs.interior print(t.summary()) if os.path.basename(t.filename) < 'prob-280': continue rows = [] for y in range(gs.Y): row = [] for x in range(gs.X): if i[x, gs.Y - y - 1]: row.append(' ') else: row.append('#') rows.append(''.join(row)) print('\n'.join(rows)) if len(input(t.summary()).strip()) > 0: break
def region_nn(worker): rm = gamestate.RegionManager(worker.gs) rm.compute_regions() next_rid = rm.regions[worker.x, worker.y] finder = gamestate.Pathfinder(worker.gs) while True: unpainted = rm.get_region(next_rid) if np.all(np.logical_not(unpainted)): if np.all(np.logical_not(worker.gs.unpainted)): return else: x, y = finder.nearest_in_array(worker.x, worker.y, worker.gs.unpainted) next_rid = rm.regions[x, y] continue x, y = finder.nearest_in_array(worker.x, worker.y, unpainted) path = finder.compute_path(worker.x, worker.y, x, y) assert len(path) > 0 for n in path[1:]: dx = n[0] - worker.x dy = n[1] - worker.y worker.move(dx, dy) if __name__ == "__main__": tasks = task.tasks_in_directory(task.part1) gs = gamestate.State(tasks[1]) gs.start() nn(gs.workers[0]) print(gs.to_string())
def nn_solver(t): gs = gamestate.State(t) plan.nn(gs.start()) return gs
def solve_mod3(task, outward_bias=0, tie_break=False, make_clones=False, verbose=False): gs = gamestate.State(task) if outward_bias > 0: tie_break = False target = np.zeros((gs.X, gs.Y, 2), dtype=int) for x in range(gs.X): for y in range(gs.Y): if x == 0: target[x, y, 0] = x target[x, y, 1] = y else: target[x, y, 0] = x - 1 target[x, y, 1] = min(gs.Y - 1, 1 + 3 * (y // 3)) if not gs.interior[target[x, y, 0], target[x, y, 1]]: target[x, y, 0] = x target[x, y, 1] = y goal = np.zeros((gs.X, gs.Y), dtype=bool) dist2 = np.zeros((gs.X + 2, gs.Y + 2), dtype=int) dist = None # np.zeros((gs.X, gs.Y), dtype = int) inf = 2 * (gs.X * gs.Y + 10) count = 0 gs.start() if tie_break or outward_bias > 0: noncentrality = solver_util.compute_centrality(gs) if make_clones: solver_util.make_clones(gs) for w in gs.workers: w.too_close = False while True: count += 1 if verbose and (count % 1000 == 0): print(np.sum(gs.unpainted)) if np.sum(gs.unpainted) == 0: if make_clones: gs2 = gs.replay_and_validate() if verbose: print("Time:", gs.time(), "truncated to", gs2.time()) return gs2 else: return gs w = gs.oldest_worker() # Are we too close to other workers? if not w.too_close: for w_ in gs.workers: if not (w is w_) and abs(w.x - w_.x) + abs(w.y - w_.y) < 3: w.too_close = True break if w.too_close: # Compute farthest unpainted point from any other worker dist2.fill(inf) for w_ in gs.workers: w_.compute_distance() if not (w is w_): dist2 = np.minimum(dist2, w_.pf.dist) dist = dist2[1:-1, 1:-1] dist[~gs.unpainted] = 0 x, y = np.unravel_index(np.argmax(dist, axis=None), dist.shape) best_dist = dist[x, y] best_tele = None for teleporter in gs.teleporters: if teleporter.time < w.time and teleporter.pf.dist[ x + 1, y + 1] + 1 < best_dist: best_dist = teleporter.pf.dist[x + 1, y + 1] + 1 best_tele = teleporter if best_tele is None: w.walk_path_to(x, y) else: w.teleport(best_tele) w.nearest_in_set(set((x, y))) w.walk_path_to(x, y) w.too_close = False else: goal.fill(False) t = target[gs.unpainted] goal[t[:, 0], t[:, 1]] = True if tie_break: xs = w.pf.all_nearest_in_array(w.x, w.y, goal) assert len(xs) > 0 x, y = xs[0] least_central = noncentrality[x, y] for x_, y_ in xs[1:]: if noncentrality[x_, y_] > least_central: x = x_ y = y_ least_central = noncentrality[x_, y_] elif outward_bias > 0: x, y = w.pf.nearest_in_array_with_bias(w.x, w.y, goal, noncentrality, outward_bias) else: x, y = w.nearest_in_array(goal) w.walk_path_to_max(x, y, 30)