Created
September 15, 2021 02:08
-
-
Save ankona/3053cf28b11607523162e3e4c45fffb9 to your computer and use it in GitHub Desktop.
scoring an n-queens board
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def n_queens_attacks(board_state: str) -> int: | |
max_attack_num = 28 | |
attacks = 0 | |
# count horizontal attacks | |
c = Counter(board_state) | |
overlap = {k: v for k, v in c.items() if v > 1} | |
for k, v in overlap.items(): | |
v_prime = v - 1 | |
num_attacks = v_prime * (v / 2) | |
attacks += num_attacks | |
# count diagonal attacks | |
for i, s in enumerate(board_state): | |
for j in range(i+1, 8): | |
col_offset = abs(i - j) | |
row_offset = abs(int(s) - int(board_state[j])) | |
# if x cols over & x rows offset - it is a diagonal attack | |
if col_offset == row_offset: | |
attacks += 1 | |
return max_attack_num - attacks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment