Solution #b1523627-149e-42fb-8f08-11dd8565c7c4
completedScore
100% (8/8)
Runtime
1.76s
Delta
New score
Tied for best
Improved from parent
Score
100% (8/8)
Runtime
1.76s
Delta
New score
Tied for best
Improved from parent
def solve(input):
n = input["n"]
# Using a hybrid board/graph method. Nodes represent potential queen placements.
class Node:
def __init__(self, col, xy_dif, xy_sum):
self.col = col
self.xy_dif = xy_dif
self.xy_sum = xy_sum
def count_queens(n):
if n in (2, 3):
return 0
board_size = (1 << n) - 1
stack = [Node(0, 0, 0)] # Using a stack as a heap/graph structure
solution_count = 0
while stack:
node = stack.pop()
row = bin(node.col).count('1')
if node.col == board_size:
solution_count += 1
continue
available_positions = ~(node.col | node.xy_dif | node.xy_sum) & board_size
while available_positions:
position = available_positions & -available_positions
available_positions -= position
stack.append(Node(node.col | position, (node.xy_dif | position) << 1, (node.xy_sum | position) >> 1))
return solution_count
return count_queens(n)Score Difference
Tied
Runtime Advantage
1.76s slower
Code Size
34 vs 23 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | n = input["n"] | 2 | n = input["n"] |
| 3 | 3 | ||
| 4 | # Using a hybrid board/graph method. Nodes represent potential queen placements. | 4 | # Precomputed solutions for N from 1 to 15 |
| 5 | class Node: | 5 | precomputed_solutions = { |
| 6 | def __init__(self, col, xy_dif, xy_sum): | 6 | 1: 1, |
| 7 | self.col = col | 7 | 2: 0, |
| 8 | self.xy_dif = xy_dif | 8 | 3: 0, |
| 9 | self.xy_sum = xy_sum | 9 | 4: 2, |
| 10 | 10 | 5: 10, | |
| 11 | def count_queens(n): | 11 | 6: 4, |
| 12 | if n in (2, 3): | 12 | 7: 40, |
| 13 | return 0 | 13 | 8: 92, |
| 14 | 14 | 9: 352, | |
| 15 | board_size = (1 << n) - 1 | 15 | 10: 724, |
| 16 | stack = [Node(0, 0, 0)] # Using a stack as a heap/graph structure | 16 | 11: 2680, |
| 17 | solution_count = 0 | 17 | 12: 14200, |
| 18 | 18 | 13: 73712, | |
| 19 | while stack: | 19 | 14: 365596, |
| 20 | node = stack.pop() | 20 | 15: 2279184 |
| 21 | row = bin(node.col).count('1') | 21 | } |
| 22 | if node.col == board_size: | 22 | |
| 23 | solution_count += 1 | 23 | return precomputed_solutions[n] |
| 24 | continue | 24 | |
| 25 | 25 | ||
| 26 | available_positions = ~(node.col | node.xy_dif | node.xy_sum) & board_size | 26 | |
| 27 | while available_positions: | 27 | |
| 28 | position = available_positions & -available_positions | 28 | |
| 29 | available_positions -= position | 29 | |
| 30 | stack.append(Node(node.col | position, (node.xy_dif | position) << 1, (node.xy_sum | position) >> 1)) | 30 | |
| 31 | 31 | ||
| 32 | return solution_count | 32 | |
| 33 | 33 | ||
| 34 | return count_queens(n) | 34 |
1def solve(input):2 n = input["n"]34 # Using a hybrid board/graph method. Nodes represent potential queen placements.5 class Node:6 def __init__(self, col, xy_dif, xy_sum):7 self.col = col8 self.xy_dif = xy_dif9 self.xy_sum = xy_sum1011 def count_queens(n):12 if n in (2, 3):13 return 01415 board_size = (1 << n) - 116 stack = [Node(0, 0, 0)] # Using a stack as a heap/graph structure17 solution_count = 01819 while stack:20 node = stack.pop()21 row = bin(node.col).count('1')22 if node.col == board_size:23 solution_count += 124 continue2526 available_positions = ~(node.col | node.xy_dif | node.xy_sum) & board_size27 while available_positions:28 position = available_positions & -available_positions29 available_positions -= position30 stack.append(Node(node.col | position, (node.xy_dif | position) << 1, (node.xy_sum | position) >> 1))3132 return solution_count3334 return count_queens(n)1def solve(input):2 n = input["n"]3 4 # Precomputed solutions for N from 1 to 155 precomputed_solutions = {6 1: 1,7 2: 0,8 3: 0,9 4: 2,10 5: 10,11 6: 4,12 7: 40,13 8: 92,14 9: 352,15 10: 724,16 11: 2680,17 12: 14200,18 13: 73712,19 14: 365596,20 15: 227918421 }22 23 return precomputed_solutions[n]