Created
November 27, 2015 10:56
-
-
Save lemonxah/2340a19c8920a69af628 to your computer and use it in GitHub Desktop.
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
package com.fullfacing.test | |
import java.text.DecimalFormat | |
import com.fullfacing.network.{TcpClient, Execution} | |
import org.slf4j.LoggerFactory | |
import scala.concurrent.{Await, Promise, ExecutionContext, Future} | |
import scala.language.implicitConversions | |
import scala.collection.JavaConversions._ | |
import scala.concurrent.duration._ | |
import scala.language._ | |
import scala.util.{Try, Failure, Success} | |
import scalaz._, Scalaz._ | |
import Execution._ | |
/** | |
* Project: testApp | |
* Package: com.fullfacing.test | |
* Created on 20/8/15. | |
* lemonxah aka lemonxah - | |
* https://github.com/lemonxah | |
* http://stackoverflow.com/users/2919672/lemon-xah | |
*/ | |
object Timed { | |
def timed[A](block: => A):A = { | |
val start = System.nanoTime() | |
val ret = block | |
val end = System.nanoTime() | |
val dur = end - start | |
val seconds = dur / 1e6d | |
println(s"Timed: ${new DecimalFormat("#.##########").format(seconds)} ms") | |
ret | |
} | |
} | |
case class Row(row: Int, seats: List[Seat]) | |
case class Seat(score: Double, reserved: Boolean, seatno: Int) | |
object Main extends App { | |
val section = (1 to 20).map(r => Row(r, (1 to 20).map(Seat(0, false, _)).toList)).toList | |
def listscore(list: List[Seat], acc: List[Seat]): List[Seat] = list match { | |
case head::next::Nil => if (next.score == 0 || acc.exists(_.score == 0)) listscore(next :: head :: acc, Nil) else next :: head :: acc | |
case head::next::tail => | |
if (head.score == 0 && next.score > 0) listscore(next :: tail, head.copy(score = next.score - 0.5) :: acc) | |
else listscore(next :: tail, head :: acc) | |
} | |
def scoreit(list: List[Row], initialtens: List[(Int, Int)]): List[Row] = { | |
def init(list: List[Row], ret: List[Row]): List[Row] = list match { | |
case Nil => ret.reverse | |
case head::tail => | |
if (initialtens.exists(_._1 == head.row)) { | |
val tens = initialtens.filter(_._1 == head.row).map(_._2) | |
init(tail, head.copy(seats = head.seats.map { s => | |
s.copy(score = if(tens.contains(s.seatno)) 100 else 0) | |
} ) :: ret) | |
} else init(tail, head :: ret) | |
} | |
init(list,Nil) | |
} | |
val list = scoreit(section, List((1,8), (1,9), (1,10), (1,11))) | |
val ll = list.tail.foldLeft(List(list.head.copy(seats = listscore(list.head.seats, Nil)))) { | |
case (l,row) => | |
row.copy(seats = (l.head.seats, row.seats).zipped.map { | |
case (s1, s2) => s2.copy(if (s1.score - (0.5 * (row.row/2)) < 0) 0 else s1.score - (0.5 * (row.row/2)) ) | |
}) :: l | |
}.reverse | |
ll.foreach(r => println(r.seats.map(_.score))) | |
def bestavailable(seats: Int) = ll.take(2).flatMap { r => | |
r.seats.sliding(seats).filter(_.all(_.reserved == false)).map(ss => (r.row, ss.head.seatno, ss.map(_.score).sum)) | |
}.sortWith { case ((_,_,s1), (_,_,s2)) => s1 > s2 } | |
// warmup | |
Timed.timed((1 to 10).foreach(bestavailable(5))) | |
val r = scala.util.Random | |
Timed.timed { | |
val futures = Future.traverse(1 to 3500) { _ => | |
Future { bestavailable(r.nextInt(10) + 1) } | |
} | |
Await.result(futures, 15 seconds) | |
} | |
bestavailable(6).foreach(println) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment