-
-
Save waynejo/24da294fdf737a1b9ac1ebdfbc14fbf1 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
import java.io.FileInputStream | |
import scala.io.StdIn | |
def minAnswer(a: Double, b: Double, c: Double): Double = | |
(-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a) | |
def maxAnswer(a: Double, b: Double, c: Double): Double = | |
(-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a) | |
def answer(time: BigInt, distance: BigInt): Int = | |
val min = minAnswer(-1, time.toDouble, -distance.toDouble) | |
val max = maxAnswer(-1, time.toDouble, -distance.toDouble) | |
val minimum = min.toInt + 1 | |
val maximum = if (max.toInt.toDouble == max) max.toInt - 1 else max.toInt | |
maximum - minimum + 1 | |
def solve6_1(inputs: Vector[String]): BigInt = | |
val pairs: Vector[(Int, Int)] = inputs(0).split(" ").map(_.trim).filter(_.nonEmpty).tail.map(_.toInt).zip(inputs(1).split(" ").map(_.trim).filter(_.nonEmpty).tail.map(_.toInt)).toVector | |
pairs.map { case (time, distance) => | |
answer(time, distance) | |
}.product | |
def solve6_2(inputs: Vector[String]): BigInt = | |
val time = BigInt(inputs(0).split(" ").map(_.trim).filter(_.nonEmpty).tail.mkString("")) | |
val distance = BigInt(inputs(1).split(" ").map(_.trim).filter(_.nonEmpty).tail.mkString("")) | |
answer(time, distance) | |
@main def solve6(): Unit = | |
val in = new FileInputStream("example6-2.in") | |
System.setIn(in) | |
val inputs = Iterator.continually(StdIn.readLine()) | |
.takeWhile(line => null != line) | |
.toVector | |
println(solve6_1(inputs)) | |
println(solve6_2(inputs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment