Skip to content

Instantly share code, notes, and snippets.

object DeceitfulWar extends App {
def war(naomiSet: Seq[Double], kenSet: Seq[Double], r: Int = 0): Int = {
if(naomiSet.isEmpty) r
else if (naomiSet.length == 1) {
if (naomiSet.head > kenSet.head) 1 else 0
}
else {
val largers = kenSet.filter(_>naomiSet.head)
if (largers.isEmpty) kenSet.size
object ChargingChaos extends App {
def flippedSwitch(outlets: Set[BigInt], devices: Set[BigInt]): Option[Int] = {
val list = outlets.toList map { outlet =>
devices.map(outlet ^ _)
}
val result = (list.head /: list.tail)(_ & _)
object ChargingChaos extends App {
def minMove(inputs: Seq[String]): Option[Int] = {
def runLength(s: String) = (List.empty[(Char, Int)] /: s) {
case ((c, n) :: tail, char) if c == char =>
(c, n + 1) :: tail
case (acc, char) =>
(char, 1) :: acc
}.reverse.unzip
object NewLotteryGame extends App {
def count(a: Long, b: Long, k: Long): Long = {
10
}
def countPairs(less: (Boolean, Boolean, Boolean), bound: List[(Boolean, Boolean, Boolean)]): Long = {
if (bound.isEmpty) {
val (a, b, k) = less
object PartElf extends App {
def minGen(p: Long, q: Long): Option[Int] = {
val Seq(a, b) = {
@annotation.tailrec
def gcd(a: Long, b: Long): Long = if (a == 0) b else gcd(b % a, a)
val g = gcd(p, q)
Seq(p, q).map(BigInt(_) / g)
object BadHorse extends App {
def isSplittable(pairs: List[(String, String)]): Boolean = {
val link = ((Map.empty[String, Set[String]] withDefaultValue Set.empty[String]) /: pairs) {
case (map, (i, j)) => map + (i -> (map(i) + j)) + (j -> (map(j) + i))
}
@annotation.tailrec
def dfs(checking: List[String], checked: Map[String, Boolean]): Boolean = checking match {
object ReadPhoneNumber extends App {
def read(numberStr: String, formatStr: String): String = {
val (numbers, format) = (numberStr map (_ - '0'), formatStr split '-' map (_.toInt))
val segments: Seq[Seq[Int]] = ((List.empty[Seq[Int]], numbers) /: format) {
case ((acc, numbers), count) => val (h, t) = numbers splitAt count; (h :: acc, t)
}._1.reverse
object RationalNumberTree extends App {
private lazy val One = BigInt(1)
private lazy val Two = BigInt(2)
@annotation.tailrec
def position(p: BigInt, q: BigInt, acc: List[Int] = Nil): BigInt = {
if (p == q) (One /: acc)(2 * _ + _)
else {
val (p0, q0, k) = if (p < q) (p, q - p, 0) else (p - q, q, 1)
object Sorting extends App {
def sort(s: Int*): Seq[Int] = {
val odds = s.filter(_ % 2 != 0).toList
val even = s.filter(_ % 2 == 0).toList
val mask = s.map(_ % 2 != 0).toList
merge(odds.sorted, even.sorted.reverse, mask)
}
@annotation.tailrec
object CrossTheMaze extends App {
class Maze(mazeLines: Vector[String]) {
case class Vec(x: Int, y: Int) { def +(v: Vec) = Vec(x + v.x, y + v.y) }
class Direction(val toChar: Char, val v: Vec) {
lazy val id: Int = directions indexOf this
lazy val searchingOrder: Stream[Direction] =
(directions ++ directions).toStream drop (id + 3) % 4 take 4