Skip to content

Instantly share code, notes, and snippets.

object SpaceshipDefence extends App {
def shortestTime(g: Map[String, Set[(String, Int)]], from: String, to: String): Int = {
import collection.immutable.SortedSet
@annotation.tailrec
def loop0(todo: SortedSet[(Int, String)], done: Set[String]): Int =
if (todo.isEmpty) -1
else todo.head match {
object SudokuChecker extends App {
def check(matrix: Vector[Vector[Int]]): Boolean = {
val nSq = matrix.size
val n = math.sqrt(nSq).toInt
val ans = (1 to nSq).toSet
(0 until nSq).forall { i: Int =>
object MeetAndParty extends App {
def locateParty(rectangularAreas: IndexedSeq[(Int, Int, Int, Int)]): (Int, Int, BigInt) = {
val houses = for {
(x1, y1, x2, y2) <- rectangularAreas
x <- x1 to x2
y <- y1 to y2
} yield (x, y)
object IgnoreAllMyComments extends App {
def ignoreComments(in: Iterator[Char])(out: Char => Unit): Unit =
((0, None) /:[(Int, Option[Char])] in) { case ((n, last), current) =>
last match {
case None => (n, Some(current))
case Some(ch) => ""+ch+current match {
case "/*" => (n+1, None)
case "*/" if n>0 => (n-1, None)
case _ =>
object Osmos extends App {
def minOps(armin: Int, motes: Seq[Int]): Int = {
@annotation.tailrec
def minOps0(armin: Int, motes: List[Int], limit: Int, count: Int = 0): Int =
if (motes.isEmpty) count
else if (count >= limit) limit
else {
object SnapperChain extends App {
def state(n: Int, k: Int): Boolean =
BigInt(k + 1) % (BigInt(2) pow n) == 0
def process(lineIn: Iterator[String])(lineOut: String => Unit) = {
for (i <- 1 to lineIn.next().toInt) {
val Array(n, k) = lineIn.next() split ' ' map (_.toInt)
object Magicka extends App {
class Solver(combines: Seq[Seq[Char]], opposeds: Seq[Seq[Char]]) {
private val comb: Map[(Char, Char), Char] = combines.flatMap {
case Seq(a, b, c) => Seq(((a, b), c), ((b, a), c))
}.toMap
private val oppo = ((Map.empty[Char, Set[Char]] withDefaultValue Set.empty) /: opposeds.flatMap {
case Seq(a, b) => Seq((a, b), (b, a))
object DiamondInheritance extends App {
def isDiamond(inheritances: Seq[Int]*): Boolean = {
val map = inheritances.zipWithIndex.map(m => (m._2 + 1, m._1)).toMap
def dfs(visiting: Int, visited: Set[Int]): Option[Set[Int]] = {
// println(s"$visiting $visited")
object FreeCellStatistics extends App {
def isPossible(n: Long, pd: Long, pg: Long): Boolean = pg match {
case 0 if pd > 0 => false
case 100 if pd < 100 => false
case _ =>
@annotation.tailrec
def gcd(a: Long, b: Long): Long = if (b == 0) a else gcd(b, a % b)
n >= 100 / gcd(pd, 100)
}
object ManageYourEnergy extends App {
def maximumGain(e: Int, r: Int, vs: Seq[Int]): Long = {
@annotation.tailrec
def maximumGain0(currentEnergy: Int, vs: List[Int], acc: Long): Long = vs match {
case Nil => acc
case x :: xs =>
val indexOfLarger = xs take e / r indexWhere x.<
val energyLeft = if (indexOfLarger == -1) 0 else currentEnergy min e - r * (indexOfLarger + 1)