Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Last active November 6, 2015 13:12
Show Gist options
  • Save sungkmi/84db97a25296c6232cb4 to your computer and use it in GitHub Desktop.
Save sungkmi/84db97a25296c6232cb4 to your computer and use it in GitHub Desktop.
object GWheels extends App {
case class Bike(pedals: Seq[Int], extras: IndexedSeq[Int], tires: Seq[Int]) {
private def normalize(n: Long, m: Long): (Long, Long) = {
def gcd(n: Long, m: Long): Long = if (m == 0) n else gcd(m, n % m)
val g = gcd(n, m)
val (a, b) = (n / g, m / g)
(a min b, a max b)
}
val extraRatios = for {
i <- (0 until (extras.size - 1)).toSet[Int]
j <- i + 1 until extras.size
} yield normalize(extras(i), extras(j))
def possible(p: Int, q: Int): Boolean = (for {
a <- pedals.toStream
c <- tires
(u, v) = normalize(a.toLong * q, c.toLong * p) if extraRatios contains (u, v)
} yield (u, v)).nonEmpty
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
def readInts() = { lineIn.next().split(' ').map(_.toInt) }
lineIn.next()
val Array(p, e, t) = readInts()
val bike = Bike(readInts(), readInts(), readInts())
lineOut(s"Case #$i: ")
for (j <- 1 to lineIn.next().toInt) {
val Array(p, q) = readInts()
lineOut(if (bike.possible(p, q)) "Yes" else "No")
}
}
val filename = "B-large-practice"
val writer = new java.io.PrintWriter(filename + ".out")
try {
process(io.Source.fromFile(filename + ".in").getLines) { s =>
writer.println(s); writer.flush()
}
} finally {
writer.flush(); writer.close()
}
}
import org.scalatest._
import GWheels._
class GWheelsTest extends FunSuite with Matchers {
test("possibleExtraRatios") {
assert(Bike(Seq(5), Vector(4, 6), Seq(3,5,7)).extraRatios === Set((2,3)))
}
test("sample #1-1") {
assert(Bike(Seq(5), Vector(4, 6), Seq(3,5,7)).possible(1, 1) === false)
}
test("sample #1-2") {
assert(Bike(Seq(5), Vector(4, 6), Seq(3,5,7)).possible(5, 2) === true)
}
test("sample case") {
val input = """1
1 2 3
5
4 6
3 5 7
2
1 1
5 2""".lines
val expected = """Case #1:
No
Yes""".lines
lineComparison(input, expected)
}
test("small #1") {
val input = """1
4 4 3
3713 140 1475 9721
5665 8611 8762 7400
5166 3739 853
10
31972643 21181435
30751629 21365175
1930185 878195
63612376 38865596
85175402 4832245
32533306 44484426
85175402 32196529
1205540 27668600
14248777 5783074
22392234 57411354""".lines
val expected = """Case #1:
Yes
No
No
No
Yes
Yes
Yes
Yes
No
No""".lines
lineComparison(input, expected)
}
test("full small case") {
val input = io.Source.fromFile("B-small-practice.in").getLines()
val expected = io.Source.fromFile("B-small-practice.out").getLines()
lineComparison(input, expected)
}
test("full large case") {
val input = io.Source.fromFile("B-large-practice.in").getLines()
val expected = io.Source.fromFile("B-large-practice.out").getLines()
lineComparison(input, expected)
}
def lineComparison(input: Iterator[String], expected: Iterator[String]) {
process(input) { s =>
for (line <- s.lines) assert(line.trim === expected.next().trim)
}
assert(expected.hasNext === false, "Finished too fast.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment