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.time.{LocalDate, LocalDateTime, LocalTime} | |
/*case */class FullName(val first: String, val last: String) | |
object FullName { | |
def apply(first: String, last: String): FullName = | |
new FullName(first, last) | |
def unapply(full: FullName): Some[(String, String)] = | |
Some((full.first, full.last)) |
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
sealed abstract class Perhaps[+A] { | |
def foreach(f: A => Unit): Unit | |
def map[B](f: A => B): Perhaps[B] | |
def flatMap[B](f: A => Perhaps[B]): Perhaps[B] | |
def withFilter(f: A => Boolean): Perhaps[A] | |
} | |
case class YesItIs[A](value: A) extends Perhaps[A] { | |
override def foreach(f: A => Unit): Unit = f(value) | |
override def map[B](f: A => B): Perhaps[B] = YesItIs(f(value)) |
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
object A extends App { | |
@inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine) | |
def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) } | |
val Array(n) = readLongs(1) | |
val Array(x, y) = readLongs(2) | |
val w = Math.max(x - 1, y - 1) | |
val b = Math.max(n - x, n - y) |
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 scala.collection.decorators._ // org.scala-lang.modules:scala-collection-contrib:0.2.0 | |
import scala.collection.immutable | |
import scala.util.Random | |
object SubsetSums extends App { | |
//val list = Array.fill(10000) { Random.nextInt(10000) + 1 } | |
val list = Array(2, 4, 8) | |
var sums = immutable.BitSet(0) | |
for (x <- list) sums |= sums << x |
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
def computeBitMasks(needle: Array[Byte]): Array[Long] = { | |
require(needle.length <= 64, "Maximum supported search pattern length is 64.") | |
val bitMasks = Array.ofDim[Long](256) | |
var bit = 1L | |
for (c <- needle) { | |
bitMasks(toUnsignedInt(c)) |= bit | |
bit <<= 1 | |
} | |
bitMasks | |
} |
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
def computeSuccessBitMask(needle: Array[Byte]): Long = { | |
1L << (needle.length - 1) | |
} |
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
def process(value: Byte): Boolean = { | |
currentMask = ((currentMask << 1) | 1) & bitMasks(toUnsignedInt(value)) | |
(currentMask & successBitMask) == 0 | |
} |
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
# JMH version: 1.22 | |
# VM version: JDK 1.8.0_242, OpenJDK 64-Bit Server VM, 25.242-b08 | |
# Processing profiler results: LinuxPerfNormProfiler | |
Benchmark (algorithm) (bufferType) Mode Cnt Score Error Units | |
findAll AHO_CORASIC HEAP thrpt 5 26.429 ± 0.034 ops/ms | |
findAll:CPI AHO_CORASIC HEAP thrpt 0.602 #/op | |
findAll:L1-dcache-load-misses AHO_CORASIC HEAP thrpt 24.653 #/op | |
findAll:L1-dcache-loads AHO_CORASIC HEAP thrpt 30099.720 #/op |
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._ | |
import java.nio.file.{Files, StandardCopyOption} | |
import scala.collection.mutable | |
import scala.collection.mutable.ArrayBuffer | |
object HashCode2020 extends App { | |
case class Book(id: Int, score: Int) | |
case class Lib(id: Int, signupDays: Int, scanPerDay: Int, books: mutable.TreeSet[Book]) |