Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
simple solution in Creative category for Pawn Brotherhood by pruidzeko
def safe_pawns(pawns):
# transform coordinates to digits, that's are convenient for calculations.
# Location for each pawn must be presented as a pair of numbers:
fn = {("abcdefgh".index(i[0]), "12345678".index(i[1])) for i in pawns}
# we can calculate the defenders pawn coordinates for each in our set:
# for each x, y in fn it is: {(x-1,y-1),(x+1,y-1)}
# end if in our set contains pawns with that coordinates
# ({(x-1,y-1),(x+1,y-1)} & fn checks such pawns-defenders in our set)
# - we summarize it by func sum() that pass in
# list of 1, generated by list comprehension expression..
return sum(1 for x,y in fn if {(x-1,y-1),(x+1,y-1)} & fn )
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
Sept. 25, 2015