Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Pawn Brotherhood by Sorrop
def safe_pawns(pawns):
letters = ['a','b','c','d','e','f','g','h']
numbers = [str(i) for i in range(1,9)]
safe_pawns = 0
for pawn in pawns:
letter,number = pawn
if number == '1':
# if pawn is in first row then it cannot have any protectors
continue
if (letter != 'h') and (letter != 'a'):
# 2 possible protectors in that case
protectors = [letters[letters.index(letter)-1] + numbers[numbers.index(number)-1],letters[letters.index(letter)+1] + numbers[numbers.index(number)-1]]
# check if they belong to the given set
if (protectors[0]) in pawns or (protectors[1] in pawns):
safe_pawns += 1
continue
elif letter == 'h':
# only one possible protector at the rightmost column of the board
protector = 'g' + numbers[numbers.index(number)-1]
if protector in pawns:
safe_pawns += 1
continue
else:
# only one possible protector at the leftmost column of the board
protector = 'b' + numbers[numbers.index(number)-1]
if protector in pawns:
safe_pawns += 1
continue
return safe_pawns
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
May 20, 2016