Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Pawn Brotherhood by sergey.tikhonov
# migrated from python 2.7
def safe_pawns(pawns):
board = [[0 for __ in range(8)] for __ in range(8)]
for p in pawns:
i = int(p[1]) - 1
j = ord(p[0]) - 97
board[i][j] = 1
result = 0
for i, row in enumerate(board):
for j, pawn in enumerate(row):
if pawn != 1 or i < 1:
continue
bottom_left = 0
bottom_right = 0
if j - 1 != -1:
bottom_left = board[i - 1][j - 1]
if j + 1 != 8:
bottom_right = board[i - 1][j + 1]
if any([bottom_left, bottom_right]):
result += 1
return result
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
July 31, 2014