Proper scala concurrency (?)
This commit is contained in:
parent
7f27119cbb
commit
b49826bc10
|
@ -1,5 +1,6 @@
|
||||||
import scala.concurrent.Future
|
|
||||||
import scala.concurrent.ExecutionContext.Implicits.global
|
import scala.concurrent.ExecutionContext.Implicits.global
|
||||||
|
import scala.concurrent.duration.Duration
|
||||||
|
import scala.concurrent.{Await, Future}
|
||||||
import scala.util.{Failure, Success}
|
import scala.util.{Failure, Success}
|
||||||
|
|
||||||
val inputLines = scala.io.Source.fromFile("input/19").getLines.toArray
|
val inputLines = scala.io.Source.fromFile("input/19").getLines.toArray
|
||||||
|
@ -24,8 +25,8 @@ def blueprintQuality(line: String, tMax: Int=24): (Int, Int, Int) =
|
||||||
|
|
||||||
val max_cost_ore = cost_orebot_ore max cost_claybot_ore max cost_obsbot_ore max cost_geodebot_ore
|
val max_cost_ore = cost_orebot_ore max cost_claybot_ore max cost_obsbot_ore max cost_geodebot_ore
|
||||||
|
|
||||||
val maxPossibleInXMinutes = (0 to 32).toArray
|
val maxPossibleInXMinutes = (0 to tMax).toArray
|
||||||
for i <- 1 to 32 do
|
for i <- 1 to tMax do
|
||||||
maxPossibleInXMinutes(i) = maxPossibleInXMinutes(i-1) + i
|
maxPossibleInXMinutes(i) = maxPossibleInXMinutes(i-1) + i
|
||||||
|
|
||||||
def simStep(t: Int, numRobots: Array[Int], numRes: Array[Int], totalGeodes: Int=0, maxGeodes: Int=0): Int =
|
def simStep(t: Int, numRobots: Array[Int], numRes: Array[Int], totalGeodes: Int=0, maxGeodes: Int=0): Int =
|
||||||
|
@ -84,20 +85,7 @@ def blueprintQuality(line: String, tMax: Int=24): (Int, Int, Int) =
|
||||||
return (bp, mostGeodes, quality)
|
return (bp, mostGeodes, quality)
|
||||||
|
|
||||||
@main def main() =
|
@main def main() =
|
||||||
// val part1 = inputLines.map(blueprintQuality(_)(2)).sum
|
val p2Futures = Future.sequence(inputLines.take(3).map(line => Future(blueprintQuality(line, 32)(1)))) // Start p2 first since p1 threads finish faster and there are only 3 p2 threads
|
||||||
// println(s"Part 1: total quality score: $part1")
|
|
||||||
// val part2 = inputLines.take(3).map(blueprintQuality(_, 32)(1)).product
|
|
||||||
// println(s"Part 2: $part2")
|
|
||||||
val p2Futures = Future.sequence(inputLines.take(3).map(line => Future(blueprintQuality(line, 32)(1))))
|
|
||||||
val p1Futures = Future.sequence(inputLines.map(line => Future(blueprintQuality(line)(2))))
|
val p1Futures = Future.sequence(inputLines.map(line => Future(blueprintQuality(line)(2))))
|
||||||
p1Futures.onComplete {
|
println(s"Part 1: total quality score: ${Await.result(p1Futures, Duration.Inf).sum}")
|
||||||
case Success(results) => println(s"Part 1: total quality score: ${results.sum}")
|
println(s"Part 2: ${Await.result(p2Futures, Duration.Inf).product}")
|
||||||
case Failure(e) => e.printStackTrace
|
|
||||||
}
|
|
||||||
p2Futures.onComplete {
|
|
||||||
case Success(results) => println(s"Part 2: ${results.product}")
|
|
||||||
case Failure(e) => e.printStackTrace
|
|
||||||
}
|
|
||||||
Thread.sleep(1000)
|
|
||||||
// val part2 = p2Futures.product
|
|
||||||
// println(s"Part 2: $part2")
|
|
||||||
|
|
Loading…
Reference in New Issue