Program to find number of possible moves to start the game to win by the starter in Python

Suppose Amal and Bimal are playing a game with n containers, each containing chocolates. The containers are numbered from 1 to N, where the ith container has count[i] chocolates. Players take turns selecting a non-empty container and removing one or more chocolates from it. The player who cannot make a move loses. We need to find how many winning first moves Amal has.

Game Rules

The game follows these rules:

  • Players alternate turns, starting with Amal
  • On each turn, a player must select a non-empty container
  • The player removes one or more chocolates from the selected container
  • The player who cannot make a move (all containers empty) loses

Example Walkthrough

For containers [2, 3], here's one possible game sequence:

  • Amal picks one chocolate from the second container: [2, 2]
  • Bimal picks one chocolate from the first container: [1, 2]
  • Amal picks one chocolate from the second container: [1, 1]
  • Bimal picks one chocolate from the first container: [0, 1]
  • Amal picks the last chocolate: [0, 0]
  • Bimal cannot move and loses

Solution Algorithm

This problem uses Nim game theory and XOR operations:

  1. Calculate the XOR of all container values (Nim-sum)
  2. If Nim-sum is 0, the current position is losing for the first player
  3. Otherwise, count winning moves where XOR with a container value creates a smaller value

Implementation

def solve(count):
    # Calculate XOR of all container values (Nim-sum)
    tmp = 0
    for c in count:
        tmp ^= c
    
    # If Nim-sum is 0, no winning moves exist
    if not tmp:
        return 0
    else:
        moves = 0
        # Count containers where XOR creates a winning position
        for c in count:
            # If XOR result is smaller than original, it's a winning move
            moves += (tmp ^ c) < c
        return moves

# Test with the example
count = [2, 3]
result = solve(count)
print(f"Input: {count}")
print(f"Number of winning first moves: {result}")
Input: [2, 3]
Number of winning first moves: 1

How It Works

The algorithm works based on Nim game theory:

  • Nim-sum: XOR of all container values determines the game state
  • Losing position: When Nim-sum is 0, the current player will lose with optimal play
  • Winning move: A move that reduces a container value such that the new XOR is smaller than the original container value

Additional Example

def solve(count):
    tmp = 0
    for c in count:
        tmp ^= c
    
    if not tmp:
        return 0
    else:
        moves = 0
        for c in count:
            moves += (tmp ^ c) < c
        return moves

# Test with different inputs
test_cases = [
    [2, 3],
    [1, 1, 1],
    [5, 7, 3],
    [4, 4]
]

for case in test_cases:
    result = solve(case)
    print(f"Containers {case}: {result} winning moves")
Containers [2, 3]: 1 winning moves
Containers [1, 1, 1]: 0 winning moves
Containers [5, 7, 3]: 2 winning moves
Containers [4, 4]: 0 winning moves

Conclusion

This problem demonstrates the application of Nim game theory using XOR operations. The key insight is that a position is winning if its Nim-sum is non-zero, and we can count valid winning moves by checking which containers allow advantageous XOR reductions.

Updated on: 2026-03-26T18:16:14+05:30

380 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements