The Dining Philosophers
<p>Five silent philosophers&nbsp;sit at a round table with bowls of spaghetti. Forks are placed between each pair of adjacent philosophers.</p>

<p>Each philosopher must alternately think and eat. However, a philosopher can only eat spaghetti when they have both left and right forks. Each fork can be held by only one philosopher and so a philosopher can use the fork only if it is not being used by another philosopher. After an individual philosopher finishes eating, they need to put down both forks so that the forks become available to others. A philosopher can take the fork on their right or the one on their left as they become available, but cannot start eating before getting both forks.</p>

<p>Eating is not limited by the remaining amounts of spaghetti or stomach space; an infinite supply and an infinite demand are assumed.</p>

<p>Design a discipline of behaviour (a concurrent algorithm) such that no philosopher will starve;&nbsp;<i>i.e.</i>, each can forever continue to alternate between eating and thinking, assuming that no philosopher can know when others may want to eat or think.</p>

<p style="text-align: center"><img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/an_illustration_of_the_dining_philosophers_problem.png" style="width: 400px; height: 415px;" /></p>

<p style="text-align: center"><em>The problem statement and the image above are taken from <a href="https://en.wikipedia.org/wiki/Dining_philosophers_problem" target="_blank">wikipedia.org</a></em></p>

<p>&nbsp;</p>

<p>The philosophers&#39; ids are numbered from <strong>0</strong> to <strong>4</strong> in a <strong>clockwise</strong> order. Implement the function&nbsp;<code>void wantsToEat(philosopher, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork)</code> where:</p>

<ul>
	<li><code>philosopher</code>&nbsp;is the id of the philosopher who wants to eat.</li>
	<li><code>pickLeftFork</code>&nbsp;and&nbsp;<code>pickRightFork</code>&nbsp;are functions you can call to pick the corresponding forks of that philosopher.</li>
	<li><code>eat</code>&nbsp;is a function you can call to let the philosopher eat once he has picked&nbsp;both forks.</li>
	<li><code>putLeftFork</code>&nbsp;and&nbsp;<code>putRightFork</code>&nbsp;are functions you can call to put down the corresponding forks of that philosopher.</li>
	<li>The philosophers are assumed to be thinking as long as they are not asking to eat (the function is not being called with their number).</li>
</ul>

<p>Five threads, each representing a philosopher, will&nbsp;simultaneously use one object of your class to simulate the process. The function may be called for the same philosopher more than once, even before the last call ends.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> [[4,2,1],[4,1,1],[0,1,1],[2,2,1],[2,1,1],[2,0,3],[2,1,2],[2,2,2],[4,0,3],[4,1,2],[0,2,1],[4,2,2],[3,2,1],[3,1,1],[0,0,3],[0,1,2],[0,2,2],[1,2,1],[1,1,1],[3,0,3],[3,1,2],[3,2,2],[1,0,3],[1,1,2],[1,2,2]]
<strong>Explanation:</strong>
n is the number of times each philosopher will call the function.
The output array describes the calls you made to the functions controlling the forks and the eat function, its format is:
output[i] = [a, b, c] (three integers)
- a is the id of a philosopher.
- b specifies the fork: {1 : left, 2 : right}.
- c specifies the operation: {1 : pick, 2 : put, 3 : eat}.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 60</code></li>
</ul>

[]
[]
[{"value": "cpp", "text": "C++", "defaultCode": "class DiningPhilosophers {\npublic:\n    DiningPhilosophers() {\n        \n    }\n\n    void wantsToEat(int philosopher,\n                    function<void()> pickLeftFork,\n                    function<void()> pickRightFork,\n                    function<void()> eat,\n                    function<void()> putLeftFork,\n                    function<void()> putRightFork) {\n\t\t\n    }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class DiningPhilosophers {\n\n    public DiningPhilosophers() {\n        \n    }\n\n    // call the run() method of any runnable to execute its code\n    public void wantsToEat(int philosopher,\n                           Runnable pickLeftFork,\n                           Runnable pickRightFork,\n                           Runnable eat,\n                           Runnable putLeftFork,\n                           Runnable putRightFork) throws InterruptedException {\n        \n    }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class DiningPhilosophers(object):\n\n    # call the functions directly to execute, for example, eat()\n    def wantsToEat(self, philosopher, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork):\n        \"\"\"\n        :type philosopher: int\n        :type pickLeftFork: method\n        :type pickRightFork: method\n        :type eat: method\n        :type putLeftFork: method\n        :type putRightFork: method\n        :rtype: void\n        \"\"\"\n        "}, {"value": "python3", "text": "Python3", "defaultCode": "class DiningPhilosophers:\n\n    # call the functions directly to execute, for example, eat()\n    def wantsToEat(self,\n                   philosopher: int,\n                   pickLeftFork: 'Callable[[], None]',\n                   pickRightFork: 'Callable[[], None]',\n                   eat: 'Callable[[], None]',\n                   putLeftFork: 'Callable[[], None]',\n                   putRightFork: 'Callable[[], None]') -> None:\n        "}]
{
  "name": "foobar",
  "params": [
    {
      "name": "target",
      "type": "integer"
    }
  ],
  "return": {
    "type": "integer"
  },
  "languages": [
    "java",
    "python",
    "python3",
    "cpp"
  ],
  "manual": true
}
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"]}
['Concurrency']