-- computes number of motes needed to absorb the next one
-- binary search the range, to get log N instead of N efficiency
computeNumMotes :: Mote -> Mote -> Int -> Int -> Int
computeNumMotes curMote tgtMote loGuess hiGuess = 
  let midGuess = (loGuess + hiGuess) `div` 2
      loSum = sumAbsorbedMotes curMote loGuess
      midSum= sumAbsorbedMotes curMote midGuess
      hiSum = sumAbsorbedMotes curMote hiGuess
  in  if loGuess + 1 >= hiGuess  -- can't binary search anymore
         then if loSum >= tgtMote
                 then loGuess
                 else if hiSum >= tgtMote
                         then hiGuess
                         else error "Probably initial bounds for guesses incorrect"
         else if loSum == tgtMote
                 then loGuess
                 else if hiSum == tgtMote
                         then hiGuess
                         else if (midSum > tgtMote)
                                 then computeNumMotes curMote tgtMote loGuess  midGuess
                                 else computeNumMotes curMote tgtMote midGuess hiGuess
computeNumMotes :: Mote -> Mote -> Int -> Int -> Int
computeNumMotes curMote tgtMote loGuess hiGuess = 
  let midGuess = (loGuess + hiGuess) `div` 2
      loSum = sumAbsorbedMotes curMote loGuess
      midSum= sumAbsorbedMotes curMote midGuess
      hiSum = sumAbsorbedMotes curMote hiGuess
  in  if loGuess + 1 >= hiGuess  -- can't binary search anymore
         then if loSum >= tgtMote
                 then loGuess
                 else if hiSum >= tgtMote
                         then hiGuess
                         else error "Probably initial bounds for guesses incorrect"
         else if loSum == tgtMote
                 then loGuess
                 else if hiSum == tgtMote
                         then hiGuess
                         else if (midSum > tgtMote)
                                 then computeNumMotes curMote tgtMote loGuess  midGuess
                                 else computeNumMotes curMote tgtMote midGuess hiGuess
computeNumMotes curMote tgtMote loGuess hiGuess = 
  let midGuess = (loGuess + hiGuess) `div` 2
      loSum = sumAbsorbedMotes curMote loGuess
      midSum= sumAbsorbedMotes curMote midGuess
      hiSum = sumAbsorbedMotes curMote hiGuess
  in  if loGuess + 1 >= hiGuess  -- can't binary search anymore
         then if loSum >= tgtMote
                 then loGuess
                 else if hiSum >= tgtMote
                         then hiGuess
                         else error "Probably initial bounds for guesses incorrect"
         else if loSum == tgtMote
                 then loGuess
                 else if hiSum == tgtMote
                         then hiGuess
                         else if (midSum > tgtMote)
                                 then computeNumMotes curMote tgtMote loGuess  midGuess
                                 else computeNumMotes curMote tgtMote midGuess hiGuess