CheckIO: Pawn Brotherhood

import itertools

def safe_pawns(pawns):
    safe_squares = itertools.chain(*[calc_safe_squares(p) for p in pawns])
    return len(pawns & set(safe_squares))
    

def calc_safe_squares(pos):
    return (chr(ord(pos[0]) - 1) + str(int(pos[1]) + 1),
            chr(ord(pos[0]) + 1) + str(int(pos[1]) + 1))

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

http://www.checkio.org/mission/pawn-brotherhood/

Leave a Reply

Your email address will not be published. Required fields are marked *