Created
October 23, 2018 15:30
-
-
Save tOverney/d3f3d6f2f982c628590f6b762e4ad569 to your computer and use it in GitHub Desktop.
Chasse 2018: Homebrewed splitwise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val MeatPrice = 95.80 | |
val HipsterMarketPrice = 66.0 | |
val CoopBecauseHipsterMarketDidntHaveEverything = 75.3 | |
val RedWinePackPrice = 136.8 | |
val NbBottlesInPack = 6 | |
val PriceForTwoRedWineBottles = 2 * RedWinePackPrice / NbBottlesInPack | |
val TotalPaidByTristan = HipsterMarketPrice + CoopBecauseHipsterMarketDidntHaveEverything + RedWinePackPrice | |
import enumeratum.{ Enum, EnumEntry } | |
sealed class Human(val alreadyPaid: Double) extends EnumEntry | |
object Human extends Enum[Human] { | |
val values = findValues | |
case object Fouki extends Human(MeatPrice) | |
// Tristan and Gaspard each kept two red wine bottles | |
case object Tristan extends Human(TotalPaidByTristan - PriceForTwoRedWineBottles) | |
case object Gaspard extends Human(-PriceForTwoRedWineBottles) | |
case object Fred extends Human(0) | |
case object Yoann extends Human(0) | |
case object Audrey extends Human(0) | |
case object Ariane extends Human(0) | |
} | |
import Human._ | |
val totalPaid = HipsterMarketPrice + CoopBecauseHipsterMarketDidntHaveEverything + PriceForTwoRedWineBottles + MeatPrice | |
val pricePerHuman = totalPaid / values.size | |
val deltas = values.map { participant => | |
val personalBalance = pricePerHuman - participant.alreadyPaid | |
val verbToUse = if (personalBalance > 0) "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