DEV Community staff staff

Posted on

Daily Challenge #147 - NIM


Nim is a mathematical strategy game in which two players take turns removing objects from piles of straw. On each turn, a player must remove at least one straw, and may remove any number straw provided they all come from the same heap/pile. The goal of the game is to take the last piece of straw.

Pile 0: ||||

Pile 1: ||

Pile 2: |||||

Pile 3: |

Pile 4: ||||||

...or more concisely: [4,2,5,1,6]


  • Each player takes turns picking a pile and removing as much straw as they want from the pile they pick.
  • Players must take at least one straw.
  • Whoever picks the last straw wins.


Write a function to create an AI that plays NIM.

You have to encode an AI in a function that takes a board represented as a list of positive integers and returns: [pile_index, number_of_straws], which refers to an index of a pile on the board and some none-zero number amount of straw to draw from that pile.





Good luck!~

This challenge comes from xcthulhu on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email with your suggestions!

Discussion (0)