Advertisement
CoffeeOnMars

Untitled

Nov 26th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.57 KB | None | 0 0
  1. class State(val adults: Int, val youngs: Int, val children: Int) {
  2.   override def toString(): String = "(a: " + adults + ", y: " + youngs + ", c: " + children + ")"
  3. }
  4.  
  5. def evolve(prevState: State): State = {
  6.   val childenPerAdult = 6 / 2 // Six children for each couple
  7.   new State(prevState.adults + prevState.youngs, prevState.children, prevState.adults * childenPerAdult)
  8. }
  9.  
  10. def stateAtTime(timeZero: State, time: Int): State = {
  11.   time match {
  12.     case 0 => timeZero
  13.     case t => evolve(stateAtTime(timeZero, t - 1))
  14.   }
  15. }
  16.  
  17. stateAtTime(new State(10, 0, 0), 3*3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement