title: Recursion pattern to find root items
body: <p>I'm trying to figure out a way to find root items and their correlation.</p>
  
  <p>In my case I have a list of "craftable Items" for a game.</p>
  
  <p>For example:</p>
  
  <ul>
  <li>woodLog</li>
  <li>woodPlank</li>
  <li>woodWall</li>
  </ul>
  
  <hr>
  
  <p>All these items have so called <code>parents</code> &amp; <code>children</code>.
  These are simple arrays of tuples that hold an <code>itemID</code> (which I can cast to an actual <code>Item</code> Object and an <code>amount</code> value.</p>
  
  <p>For example:</p>
  
  <pre><code>typealias ParentTuple = (id: Int, amount: Float)
  typealias ChildrenTuple = (id: Int, amount: Float)
  
  var woodLog = Item(id: 1,
                     name: "Wood Log",
                     parents: [],
                     children: [ChildrenTuple(id: 2, amount: 2)]
                     )
  
  var woodPlank = Item(id: 2,
                       name: "Wood Plank",
                       parents: [ParentTuple(id: 1, amount: 0.5)],
                       children: [ChildrenTuple(id: 3, amount: 4)]
                       )
  
  var woodStick = Item(id: 3,
                       name: "Wood Stick",
                       parents: [ParentTuple(id: 2, amount: 0.25)],
                       children: []
                       )
  </code></pre>
  
  <p>As you can see a Wood Log can create 2 Wood Planks, while you need 0.5 Wood Logs to create 1 Wood Plank. (later I will ceil every value to the next full integer of course).</p>
  
  <p>So following this logic from 1 Wood Log you should be able to craft 8 Wood Sticks.</p>
  
  <ul>
  <li>1x Wood Log -></li>
  <li>2x Wood Plank -></li>
  <li>8x Wood Stick</li>
  </ul>
  
  <p>Now what I need to know is how many Wood Logs would I need If I want to example craft 32 Wood Sticks?</p>
  
  <p>I managed to recursively return a list of all <code>rootItems</code> (items that do not have any <code>parents</code>) of an item like so:</p>
  
  <pre><code>func rootItems() -&gt; [ParentTuple] {
      guard self.isCraftable else { return [ParentTuple(self.id, amount: 1)] }
  
      var rootItems = [ParentTuple]()
  
      for (_, parentTuple) in self.parentIDs.enumerated() {
          guard let parentItem = Item.item(forID: parentTuple.id) else {
              assertionFailure()
              return [ParentTuple(self.id, amount: 1)]
          }
          if parentItem.isRootItem {
              rootItems.append(ParentTuple(id: parentTuple.id, amount: parentTuple.amount))
          } else {
              rootItems.append(contentsOf: parentItem.rootItems())
          }
      }
  
      return rootItems
  }
  </code></pre>
  
  <p>This works great. I'm just stuck on how I would get the <code>amount</code> to work.
  In theory for every recursive call I'd just have to multiply the current <code>parentTuple.amount</code> with the next one (in our example: 0.25 * 0.5 = 0.125 --> 1/8 Wood Log for 1 Wood Branch)</p>
  
  <p>Is my model maybe not suited for that task, am I missing something?</p>