Advertisement
cyberjab

Nim solver against bot

Oct 4th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. from copy import deepcopy
  2.  
  3. def calc_xor(sp):
  4. mxor = 0
  5. for x in sp:
  6. mxor ^= x
  7. return mxor
  8.  
  9. sp = [int(x) for x in input().split()]
  10. p = 0
  11. while sum(sp) > 0:
  12. if p == 0:
  13. copy_sp = deepcopy(sp)
  14. f = False
  15. for i in range(len(copy_sp)):
  16. if f:
  17. break
  18. for val in range(0, copy_sp[i] + 1):
  19. copy_sp[i] = val
  20. xxor = calc_xor(copy_sp)
  21. if xxor == 0:
  22. f = True
  23. # left this many rocks
  24. print(i, val)
  25. sp = copy_sp
  26. break
  27. p += 1
  28. else:
  29. # bot turn in format heap[i] = val (i,x)
  30. i, x = [int(x) for x in input().split()]
  31. sp[i] = x
  32. p -= 1
Tags: Nim
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement