Skip to content

Instantly share code, notes, and snippets.

View linasm's full-sized avatar

Linas Medžiūnas linasm

View GitHub Profile
@linasm
linasm / HashCode2020.scala
Created February 28, 2020 12:32
Google Hash Code 2020 solution (total score 26 909 323)
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])
@linasm
linasm / 31d573d116a6d7ef8148ab83a621b159d3fa6edf.txt
Last active February 26, 2020 07:17
Netty benchmark results
# 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
@linasm
linasm / ShiftingBitMaskSearch.scala
Created January 17, 2020 13:20
Success bit mask precomputing for Shifting Bit Mask string search algorithm
def process(value: Byte): Boolean = {
currentMask = ((currentMask << 1) | 1) & bitMasks(toUnsignedInt(value))
(currentMask & successBitMask) == 0
}
@linasm
linasm / ShiftingBitMasksPrecompute2.scala
Created January 17, 2020 13:04
Success bit mask precomputing for Shifting Bit Mask string search algorithm
def computeSuccessBitMask(needle: Array[Byte]): Long = {
1L << (needle.length - 1)
}
@linasm
linasm / ShiftingBitMasksPrecompute1.scala
Last active January 17, 2020 13:25
Bit mask precomputing for Shifting Bit Mask string search algorithm
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
}
@linasm
linasm / SubsetSumBitSet
Created September 14, 2019 18:28
Subset Sum using BitSet
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
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)
@linasm
linasm / forcomp.scala
Last active October 8, 2021 20:59
Comprehending the For-Comphension talk, Vilnius, Nov 2018
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))
@linasm
linasm / unapply.scala
Last active March 15, 2021 06:17
Output of live coding session "Scala pattern matching: apply the unapply"
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))