Skip to content

Instantly share code, notes, and snippets.

@tOverney
Created October 13, 2019 14:01
Show Gist options
  • Save tOverney/f131f34ac652056c6fea03c98246b0ad to your computer and use it in GitHub Desktop.
Save tOverney/f131f34ac652056c6fea03c98246b0ad to your computer and use it in GitHub Desktop.
Chasse 2019
val MeatPrice = 137.25D
val HipsterMarketPrice = 14.9D
val MigrosBecauseHipsterMarketDidntHaveEverything = 56.45D
val RedWineTwoBottlePrice = 33D
val ChanterellesAightss = 14D
sealed class Human(val alreadyPaid: Double)
object Human {
val values = Set(Fouki, Tristan, Gaspard, Fred, Yoann, Audrey, Ariane)
case object Fouki extends Human(0D)
case object Tristan extends Human(MigrosBecauseHipsterMarketDidntHaveEverything)
case object Gaspard extends Human(0D)
case object Fred extends Human(MeatPrice)
case object Yoann extends Human(0D)
case object Audrey extends Human(RedWineTwoBottlePrice + ChanterellesAightss + HipsterMarketPrice)
case object Ariane extends Human(0D)
}
import Human._
val totalPaid = HipsterMarketPrice + MigrosBecauseHipsterMarketDidntHaveEverything + RedWineTwoBottlePrice + MeatPrice + ChanterellesAightss
val pricePerHuman = totalPaid / values.size
val deltas = values.map { participant =>
val personalBalance = pricePerHuman - participant.alreadyPaid
val verbToUse = if (personalBalance > 0D) "must pay" else "is owed"
println(f"$participant $verbToUse ${Math.abs(personalBalance)}%.2f CHF.")
personalBalance
}
require(deltas.sum < 0.001, "Depbts and credits must amount to 0 (or close to it because of floating point precision)!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment