'Count available unique combinations when each option has a limit

I need algorithm that count's available unique combinations, but I can't wrap my head around it. Let's say I have multiple categories and each category have multiple options and then each option has limit how many times it can be used.

For example:

Shape

  • Rectangle (4)
  • Triangle (3)
  • Circle (2)
  • Hexagon (1)

Material

  • Wood (5)
  • Stone (3)
  • Metal (2)

Color

  • Blue (7)
  • Green (2)
  • Red (1)

There can be different number of options in each category, but the sum is contstant (10 in this example).

Now I need to count how many possible unique combinations can be there.

Let's say first combination is rectangle, stone, red. Now balance for each option should be reduced by 1 and red should be removed from available options in color category. Next combination is hexagon, metal, green. Hexagon is removed from shape category and so on...

In the end function should return number of available unique combinations.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source