Below is the Pirate riddle in its most common form, with 5 pirates. Adapted from Popular Mechanics.
Five pirates are to divide 100 gold coins among themselves. The pirates have a system that they are sworn to follow. It works like this: The captain proposes a way to divide the coins. Then all the pirates, including the captain, vote on whether or not to accept the proposal. If a majority accept or there is a tie they divvy the coins as proposed. Otherwise the captain is killed and the next ranking pirate takes over as captain. The new captain makes a proposal and they vote again with the same rules.
There are a few important things to know about the pirates. First, they will make their decisions according to whatever will net them the most money without getting them killed. Second, all other things being equal, they would prefer to kill the captain. Third, they do not trust each other in the slightest and will make no agreements amongst themselves to determine how they vote. Finally, the pirates are perfect logicians, capable of considering all possible outcomes.
Let's consider the pirates A, B, C, D and E, with A being the captain and the rest of the pirates ordered by rank. How should Pirate A suggest they divvy up the coins?
The code in this repo solves this riddle with an arbitrary number of pirates. The 100 coins of the original riddle can only be distributed optimally to a maximum of 200 pirates. After that the solver increases the total number of coins by one for each pirate over number 200.
Run the solver using stack:
stack build
stack exec pirate-riddle-exe -- 5
Run the Hspec test suite:
stack test