コード例 #1
0
def iterate_input(grid: Grid) -> Grid:
    all_sets = [point.neighbours() for point in grid.keys()]
    all_points_to_consider = set.union(*all_sets)
    new_grid: Grid = {}
    for point in all_points_to_consider:
        currently_active = is_active(point, grid)
        active_count = count_where(lambda pt: is_active(pt, grid), point.neighbours())
        now_active = compute_new_state(currently_active, active_count)
        new_grid[point] = now_active

    return new_grid
コード例 #2
0
def count_trees_for_slope(input_lines: List[str], x_slope: int,
                          y_slope: int) -> int:
    row_count: int = len(input_lines)
    rows_visited: List[str] = [
        input_lines[ix] for ix in range(0, row_count, y_slope)
    ]

    x_coords: iter = count(0, x_slope)
    path: List[str] = [
        get_item_at_location(row, next(x_coords)) for row in rows_visited
    ]
    return count_where(is_tree, path)
コード例 #3
0
def count_strict_valid_passports(file_name: str) -> int:
    parsed_passports = parse_passports_from_file(file_name)
    return count_where(is_strict_valid, parsed_passports)
コード例 #4
0
def count_occupied(seat_list: List[str]) -> int:
    return count_where(is_occupied, seat_list)
コード例 #5
0
def get_visible_occupied_seats(grid: Grid, point: Point) -> int:
    directions = get_unit_directions()
    seats = [get_seat_for_direction(grid, point, d) for d in directions]
    return count_where(is_occupied, seats)
コード例 #6
0
def count_safe_ingredient_appearances(input_lines: List[str]) -> int:
    safe = get_safe_ingredients(input_lines)
    all_ingredient_occurrences: List[Ingredient] = get_all_ingredients_flat(
        input_lines)
    return count_where(lambda ingredient: ingredient in safe,
                       all_ingredient_occurrences)
コード例 #7
0
def count_valids(rules: dict[int, Rule], messages: List[str]) -> int:
    return count_where(lambda message: is_valid(rules, message), messages)
コード例 #8
0
def read_and_boot(file_name: str, parse_as_grid: Callable[[str], Grid]) -> int:
    grid: Grid = parse_as_grid(file_name)
    for i in range(0, 6):
        grid = iterate_input(grid)

    return count_where(lambda pt: is_active(pt, grid), grid.keys())
コード例 #9
0
def count_valid_new_way(password_list: List[str]) -> int:
    parsed_lines: List[PasswordAndRule] = parse_lines(password_list)
    return count_where(is_valid_new_way, parsed_lines)
コード例 #10
0
 def count_sea_monsters(self) -> int:
     return count_where(self.has_sea_monster, self.grid.keys())
コード例 #11
0
 def get_water_roughness(self) -> int:
     hash_count: int = count_where(lambda key: self.grid[key] == '#', self.grid.keys())
     return hash_count - (self.count_sea_monsters() * len(sea_monster_points))
コード例 #12
0
def calculate_for_n(n: int) -> int:
    number_strs = [str(i) for i in range(1, n+1)]
    illegal_parts: List[str] = [''.join(seq) for seq in windowed(number_strs, 2)]
    all_permutations = [''.join(perm) for perm in permutations(number_strs, n)]
    return count_where(lambda permutation: is_allowed(permutation, illegal_parts), all_permutations)
コード例 #13
0
def iterate_tile(tile: Hex, black_tiles: Set[Hex]) -> bool:
    currently_black = tile in black_tiles
    neighbour_black_count = count_where(
        lambda neighbour: neighbour in black_tiles, tile.neighbours())
    return flip_tile(currently_black, neighbour_black_count)