Example #1
0
def dict_sum(a: defaultdict, b: dict, inplace: bool = True) -> defaultdict:
    """
    Calculate a + b, key-by-key.
    """
    if not inplace:
        result = a.copy()
    else:
        result = a
    for key, value in b.items():
        result[key] += value
    return result
Example #2
0
def process_tiles(tiles: defaultdict) -> defaultdict:
    tiles_copy = tiles.copy()
    relevant_tiles = [get_relevant_tiles(coord) for coord in tiles.keys()]
    relevant_tiles = set(reduce(add, relevant_tiles))

    for tile_coord in relevant_tiles:
        neighbor_value = get_black_neighbors(tiles, tile_coord)
        black_color = tile_coord in tiles.keys()
        if (black_color and (neighbor_value == 0 or neighbor_value > 2)
                or not black_color and neighbor_value == 2):
            tiles_copy = switch_color(tiles_copy, tile_coord)

    return tiles_copy
Example #3
0
def to_next_inter(pos, cmds: list, blocked: defaultdict, turn, dir):
    blocked = blocked.copy()
    cmds = cmds.copy()
    # print(pos, cmds, turn, dir)
    if turn:
        dir = dir_turn(dir, turn)

    move_dir = directions[dir]
    pathlen = 0
    done = False
    # print(new_dir, move_dir)
    if len(cmds) >= 18:
        pathlist.append(cmds)
        return
    if pos == goal:
        pathlist.append(cmds)
        return
    while not done:
        nb = pos[0] + move_dir[0], pos[1] + move_dir[1]
        pathlen += 1
        blocked[pos] = True
        if nb in corners or nb in intersections:
            done = True
            cmds.extend([turn, pathlen])
            new_pos = [(a, b) for a, b in get_nbs(nb)
                       if (a, b) != pos and grid[(
                           a, b)] == SCAFFOLD and not blocked[(a, b)]]
            if len(new_pos) == 0:
                return
            for new in new_pos:
                if dir == U:
                    turn = 'R' if nb[0] < new[0] else 'L'
                    turn = None if nb[1] > new[1] else turn
                elif dir == R:
                    turn = 'R' if nb[1] < new[1] else 'L'
                    # turn = None if nb[1] > new[1] else turn
                elif dir == D:
                    turn = 'R' if nb[0] > new[0] else 'L'
                elif dir == L:
                    turn = 'R' if nb[1] > new[1] else 'L'
                # print("TURN", turn, pos, new)
                print(pos, cmds)
                to_next_inter(nb, cmds, blocked, turn, dir)
        pos = nb
def normalise_dict(num_value_dict: defaultdict) -> defaultdict:
    norm = sum(num_value_dict.values())
    normed_res = num_value_dict.copy()
    for k, v in normed_res.items():
        normed_res[k] = v / norm
    return normed_res
Example #5
0
 def __init__(self, actions: List[str], moves: defaultdict):
     self.__actions = actions.copy()
     self.__moves = moves.copy()
     self.__action_index, self.__action_value = self.__make_action_index_n_value(
     )
     return