def explore(self): if self.steps > 1800: raise StopIteration for i, robot in enumerate(self.robots): r = robot.copy() old_keys = r.owned t = lambda x: len(x.owned) > len(old_keys) key = State.hashfun(r) if key in self.memo: for solution in self.memo[key]: found = solution.state.owned - old_keys new_robots = [r.copy() for r in self.robots] new_robots[i] = solution.state.copy() yield JointSearch(new_robots, steps=self.steps + solution.step, ordered_keys=self.ordered_keys + (found.pop(), )) continue game = Game(r, State.f, t, hashfun=State.hashfun, solution_limit=None, verbose=False) s = game.solve() for solution in s: self.memo[key].add(solution) found = solution.state.owned - old_keys new_robots = [r.copy() for r in self.robots] new_robots[i] = solution.state.copy() yield JointSearch(new_robots, steps=self.steps + solution.step, ordered_keys=self.ordered_keys + (found.pop(), ))
def allinitmoves(): game = Game() #game.print() #game.printcoords() nummoves = len(list(game.move_gen(0))) #print(nummoves) for i in range(nummoves): game = Game() moves = list(game.move_gen(0)) move = moves[i] #print(move) result = game.move(*move[:2]) #print(result) game.print(stats=False) print("") print(nummoves)
def npcgame(): game = Game() game.print() round = 0 colors = [0, 1] while not game.is_over(): if round % 2 == 0: #result = game.randommove() result = game.aimove() else: result = game.aimove(debug=True) #if round % 100 == 0: #print(result) #game.print() game.print(mode=0) if result[0]: round += 1 print(game.out) print(round)
target, zero, used = s return round(100 * distance((0, 0), target) + distance(zero, target)) def hashfun(s, step): target, zero, used = s return (target, zero) if __name__ == '__main__': p = re.compile(r"(\d+)-y(\d+)\D*(\d+)\D*(\d+)\D*(\d+)\D*(\d+)") used = [[None for _ in range(Nx)] for _ in range(Ny)] with open("22.txt") as f: ls = f.readlines() for line in ls[2:]: m = p.search(line) data = tuple(map(int, m.groups())) x, y = data[:2] SIZES[y][x] = data[2] used[y][x] = data[3] viable = len(list(viable_pairs(used))) print(viable) target = (0, Nx - 1) zero = (25, 20) s0 = (target, zero, tuple(tuple(row) for row in used)) game = Game(s0, expander, t, h=h, min_h=True, hashfun=hashfun) s2 = game.solve() print(s2[1])
def explore(self, pos): for i, move in enumerate(self.MOVEMENT, start=1): dest = np.array(pos) + move t = tuple(dest) program = self.programs[pos] new_program = program.copy() new_program.queue.append(i) new_program.output_callback = lambda v: self.output_callback( dest, v) self.programs[t] = new_program try: new_program.execute() except intcode.NoInputException: pass if self.world[t] != -1: yield t def is_goal(self, pos): return self.world[tuple(pos)] == 1 if __name__ == '__main__': program = intcode.IntCode.from_file("15.txt") robot = Robot(program) game = Game(robot.s0, robot.explore, robot.is_goal, stop_condition=False) s = game.solve() print(s) time = robot.fill_oxygen() print(time)
return -(abs(x - d[0]) + abs(y - d[1])) def t(state): return state == d def print_track(coords): edge = max(chain.from_iterable(coords)) + 1 map = '' for y in range(edge): for x in range(edge): if is_wall(x, y): map += ':' elif (x, y) in coords: map += 'O' else: map += ' ' map += '\n' print(map) if __name__ == '__main__': game1 = Game(s0, f, t, h=h) s, step = game1.solve() print_track(s) print(len(s) - 1) game2 = Game(s0, f, t, step_limit=50) game2.solve() print(len(game2.visited))
map_length = len(ascii_codes) ascii_codes.clear() def print_live_feed(v): if v > 1000: print(v) return ascii_codes.append(v) if len(ascii_codes) <= map_length: pass else: s = ''.join(chr(c) for c in ascii_codes) ascii_codes.clear() print(s) program.output_callback = print_live_feed program.reset() s0 = State(pos, direction, []) game = Game(s0, State.f, State.t, h=State.h, hashfun=State.hashfun, solution_limit=1) solution = game.solve() movement_program = solution[0][0][-1].main_program program.queue.extend(movement_program.main_function) program.queue.extend(chain(*movement_program.programs)) program.queue.extend([ord('n'), ord('\n')]) program.execute()
start = t elif c == '#': walls.add(t) elif c.islower(): keys[t] = c elif c.isupper(): doors[t] = c.lower() return cls(start, keys, doors, walls, bounds=t, pos=start, controller=controller) if __name__ == '__main__': # s0 = State.from_file("18.txt") # game = Game(s0, State.f, State.t, hashfun=State.hashfun, solution_limit=1) # s = game.solve()[0] # print(s.step) s0 = JointSearch.from_file("18_bis.txt") game = Game(s0, JointSearch.f, JointSearch.t, h=JointSearch.h, hashfun=JointSearch.hashfun, verbose=True) ss = game.solve() print(min(s.state.steps for s in ss))
def pvsnpcgame(premoves=None): game = Game() #game.print() #game.print(mode=3) if isinstance(premoves, str): for line in premoves.split("\n"): line = line.strip() if len(line) == 0: continue print(line) game.move(*game.move_from_str(line)) game.aimove() game.printsbs() round = 0 colors = [0, 1] try: while not game.is_over(): if game.next_color == 0: while True: try: inp = input(">") move = game.move_from_str(inp) result = game.move(*move) if result[0]: break else: print(result) except Exception as e: print(e) else: result = game.aimove(debug=False) #if round % 100 == 0: #print(result) #game.print() #game.print(mode=0) #game.print(mode=3) game.printsbs() if result[0]: round += 1 else: notice = "ball out" if result[2] else "e" print(result) except KeyboardInterrupt: pass print(game.backup_history()) print(game.out) print(round)
def test(): game = Game() game.printcoords() game.print() print(len(list(game.move_gen())))
def npcvsnpcgame(premoves=None): game = Game() #game.print() #game.print(mode=3) if isinstance(premoves, str): for line in premoves.split("\n"): line = line.strip() if len(line) == 0: continue print(line) game.move(*game.move_from_str(line)) game.aimove() game.printsbs() round = 0 colors = [0, 1] try: while not game.is_over(): result = game.aimove(debug=False) print(game.backup_history()) #if round % 100 == 0: #print(result) #game.print() #game.print(mode=0) #game.print(mode=3) game.printsbs() if result[0]: round += 1 else: notice = "ball out" if result[2] else "e" print(result) except KeyboardInterrupt: pass print(game.backup_history()) print(round)
yield (x, y) def t(s): return s == curr_target if __name__ == '__main__': with open("24.txt") as f: ls = f.readlines() for y, line in enumerate(ls[1:-1]): for x, c in enumerate(line[1:-1]): d[x][y] = c != '#' if c.isnumeric(): n = int(c) flags[n] = (x, y) minpaths = {} for fs, fe in combinations(range(8), r=2): start, end = flags[fs], flags[fe] curr_target = end g = Game(start, explorer, t) s = g.solve() minpaths[(start, end)] = minpaths[(end, start)] = s[1] s1 = None for path in permutations(flags[1:]): l = sum(minpaths[(s, e)] for s, e in zip((flags[0], ) + path, path)) l += minpaths[(path[-1], flags[0])] s1 = l if s1 is None else min(s1, l) print(s1)
self.outer = is_outer def __str__(self): return self.name def __repr__(self): return str(self) def __hash__(self): return hash(self.name) def link(self, other): if other.entrance != self.entrance: self.links[self.entrance] = other.entrance self.links[other.entrance] = self.entrance other.links[other.entrance] = self.entrance other.links[self.entrance] = other.entrance if __name__ == '__main__': s0 = Maze.from_file("20.txt") game = Game(s0, Maze.f, Maze.t, h=Maze.h, min_h=True, hashfun=Maze.hashfun, verbose=True) ss = game.solve() print(ss[0].step + len(ss[0].state.visited_portals))