Beispiel #1
0
                continue
            if avail >= used:
                pairs[x, y].add((i, j))

    return grid, pairs


def moves_func(grid, state):
    empty, target = state
    transfers = [(x, y) for x, y in neighbors4(empty) if (x, y) in grid]
    for n in transfers:
        if grid[empty][SIZE] > grid[n][USED]:
            yield (n, empty if n == target else target)


def heur_func(state):
    _, target = state
    return cityblock_distance(target)


if __name__ == '__main__':
    grid, pairs = parse(Input(22))
    print(sum(len(s) for s in pairs.values()))
    empty = [n for n in grid if grid[n][USED] == 0][0]
    moves_func = functools.partial(moves_func, grid)
    maxx = max(n[0] for n in grid)
    start = (empty, (maxx, 0))
    path = astar_search(start, heur_func, moves_func=moves_func)
    print(path)
    print(len(path) - 1)
Beispiel #2
0
def h_func(state):
    point, path = state
    return cityblock_distance(point, q=(3, -3)) * len(path)


def longest_depth_first(state):
    point, path = state
    if point == (3, -3):
        return state, len(path) - 8

    best = None
    for s in get_moves(state, t=False):
        ans = longest_depth_first(s)
        if not ans:
            continue
        p, path_length = ans
        if not best:
            best = p, path_length
        if path_length > best[1]:
            best = (p, path_length)
    return best


if __name__ == '__main__':
    start = ((0, 0), 'bwnlcvfs')
    p = astar_search(start, moves_func=get_moves, h_func=h_func)
    pprint(p)
    print(len(p) - 1)
    print(longest_depth_first(start))
Beispiel #3
0
            if c not in '#.':
                poi.append(c)
    return grid, start, poi


def moves_func(grid, state):
    location, visited = state
    for p in neighbors4(location):
        v = grid[p]
        if v == '#':
            continue
        if v == '.':
            yield (p, visited)
        else:
            yield (p, visited | {v})


def heur(poi, start, state):
    location, visited = state
    return (len(poi) - len(visited)) + (location != start)


if __name__ == '__main__':
    grid, start_point, goals = parse_grid(Input(24))
    start = (start_point, frozenset('0'))
    moves = partial(moves_func, grid)
    h = partial(heur, goals, start_point)
    p = astar_search(start, h_func=h, moves_func=moves)
    # print(p)
    print(len(p) - 1)
Beispiel #4
0
        x, y = p
        if x < 0 or y < 0:
            continue
        o = bin(x * x + 3 * x + 2 * x * y + y + y * y + 1352).count('1') % 2
        if not o:
            yield p


def fill(start, moves_func):
    points = {start}
    paths = deque()
    paths.append([start])
    while paths:
        p = paths.popleft()
        if len(p) <= 50:
            moves = moves_func(p[-1])
            for move in moves:
                if move not in points:
                    points.add(move)
                    paths.append(p[:] + [move])
    return points


if __name__ == '__main__':
    h_func = lambda x: cityblock_distance(x, (31, 39))
    path = astar_search((1, 1), moves_func=get_moves, h_func=h_func)
    print(path)
    print(len(path) - 1)
    points = fill((1, 1), get_moves)
    print(len(points))
Beispiel #5
0
                next_floor = state.floor[e] | set(obj)
                if not valid_floor(prev_floor) or not valid_floor(next_floor):
                    continue
                floors = list(state.floor)
                floors[state.elevator] = prev_floor
                floors[e] = next_floor
                yield State(e, tuple(floors))

def valid_floor(floor):
    chips = [x[0] for x in floor if x[1].startswith('micro')]
    gens = [x[0] for x in floor if x[1].startswith('gen')]
    if gens:
        if chips:
            if any(c not in gens for c in chips):
                return False
    return True

def sum_dist_from_top(state):
    total = 0
    for value, floor in enumerate(reversed(state.floor)):
        total += value*len(floor)
    return total


if __name__ == '__main__':
    start = initalize(Input(11))
    pprint(start)
    path = astar_search(start, h_func=sum_dist_from_top, moves_func=get_moves)
    pprint(path)
    print(len(path)-1, ' steps needed.')