Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Pawn Brotherhood by zoido
import itertools
from typing import NamedTuple, Set
LETTERS_OFFSET = ord("a") - 1
class Position(NamedTuple):
x: int
y: int
def _to_positions(str_coordinates: Set[str]):
coordinates: Set[tuple] = set()
for c in str_coordinates:
x, y = tuple(c)
position = Position(x=ord(x) - LETTERS_OFFSET, y=int(y))
coordinates.add(position)
return coordinates
def _reachable_coordinates(position: Position) -> Set[Position]:
return {
Position(x=position.x - 1, y=position.y + 1),
Position(x=position.x + 1, y=position.y + 1),
}
def safe_pawns(str_coordinates: Set[str]) -> int:
coordinates = _to_positions(str_coordinates)
safe_coordinates = set(
itertools.chain.from_iterable(
_reachable_coordinates(p) for p in coordinates))
return len(coordinates & safe_coordinates)
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert safe_pawns({"b4", "d4", "f4", "c3", "e3", "g5", "d2"}) == 6
assert safe_pawns({"b4", "c4", "d4", "e4", "f4", "g4", "e5"}) == 1
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
Oct. 14, 2017