Skip to content

Instantly share code, notes, and snippets.

@md2perpe
Created November 30, 2020 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save md2perpe/972c9f1a6fbfd30db3a151c75f7a8499 to your computer and use it in GitHub Desktop.
Save md2perpe/972c9f1a6fbfd30db3a151c75f7a8499 to your computer and use it in GitHub Desktop.
# Maximum unattacked squares
def attacked_from_square(r, c):
attacked = set([])
for i in range(5):
attacked.add((r,i))
attacked.add((i,c))
if 0<=r+i<5 and 0<=c+i<5:
attacked.add((r+i,c+i))
if 0<=r+i<5 and 0<=c-i<5:
attacked.add((r+i,c-i))
if 0<=r-i<5 and 0<=c+i<5:
attacked.add((r-i,c+i))
if 0<=r-i<5 and 0<=c-i<5:
attacked.add((r-i,c-i))
return attacked
def count_unattacked(board):
attacked = set([])
for i in board:
attacked.update(attacked_from_square(i//5, i%5))
return 25 - len(attacked)
import itertools
max_ = 0
for board in itertools.combinations(range(25), 5):
new = count_unattacked(board)
if new > max_:
max_ = new
print(f"{max_}: {board}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment