Skip to content

Instantly share code, notes, and snippets.

@krikit
Created April 1, 2016 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krikit/e5c9b12640a3987a9c0038b1c0b09b39 to your computer and use it in GitHub Desktop.
Save krikit/e5c9b12640a3987a9c0038b1c0b09b39 to your computer and use it in GitHub Desktop.
object CodysJam extends App {
def removeOrigPrice(prices: IndexedSeq[Long], discountedPrice: Long): IndexedSeq[Long] = {
val idx = prices.indexOf(discountedPrice * 4 / 3)
prices.slice(0, idx) ++ prices.slice(idx + 1, prices.size)
}
def getDiscounted(prices: IndexedSeq[Long]): IndexedSeq[Long] = {
if (prices.isEmpty) IndexedSeq()
else Array(prices.head) ++ getDiscounted(removeOrigPrice(prices.tail, prices.head))
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
lineIn.next() // number of prices
val prices = lineIn.next().split(" ").map(_.toLong)
lineOut(s"Case #$i: ${getDiscounted(prices).mkString(" ")}")
}
val filename = "A-small-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()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment