Eating Lions, Wolves, and Goats Faster
I believe finding the single optimal solution wasn't the point - as others have pointed out ( for a very good solution, see: https://news.ycombinator.com/item?id=7856275 ), one can find that by hand - you don't need to write any code for that. The OP tried to exhaustively enumerate all possible unique forests (he considered a forest unique if the (lion,wolf,goat) triple was unique ). That obviously means some data structure has to track all these unique forests - store them, ensure uniqueness etc. Usually this is some implementation of a set. In Scala for example, a HashSet[(Int,Int,Int)] would be the obvious choice. Unfortunately, finding all these forests & adding them to the set to ensure uniqueness will also ensure your code buckles down to its knees. That's why with a triple of (2055,2006,2017), you get 1,448,575,636 unique triples - finding 1.4 billion unique triples will surely slow you down.
Or you could do it even faster by solving it directly. First note that all the parities change together, so pick the two smallest groups with the same parity, say wolves and goats. These will be the species to try to eliminate.
Then have all the wolves eat goats until only one of the species is left, say 2k wolves. Then do k iterations of lion eat wolf and wolf eat goat.
This was surprising to see here. unRisk is a hugely technical company, doing quant finance with Mathematica. Not the type of thing you normally find here.
As to the problem, the second I started reading I knew it would be an operations research problem. Though, to be honest I thought there would be a closed form solution
Nice! I managed to get a javascript solution down to 0.388s (https://gist.github.com/curveship/f1f7155ef5243af18bc2), but this is considerably faster.