Skip to content

Instantly share code, notes, and snippets.

@mkulak
mkulak / moba.kt
Created November 10, 2014 18:14
kotlin moba task
package moba_teams
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.FileInputStream
import java.util.ArrayList
import komino.Bone
import komino.format
import komino.deque
import java.io.File
@mkulak
mkulak / notify-flash-error.py
Created December 17, 2011 21:32
Notify about flash errors
import os
import time
filename = "/home/username/.macromedia/Flash_Player/Logs/flashlog.txt"
old_size= os.path.getsize(filename)
while True:
time.sleep(1)
new_size = os.path.getsize(filename)
if old_size != new_size:
f = open(filename)
@mkulak
mkulak / gist:1665398
Created January 23, 2012 20:32
Darkus january '12 programming contest solution
def decode(key: Seq[Char], bytes: Array[Byte]): String = new String(bytes.indices.map(i => bytes(i) ^ key(i % key.length)).map(_.toByte).toArray, "latin1")
val bytes = Source.fromFile("Code.hs.enc", "latin1").mkString.getBytes("latin1")
val res = bytes.sliding(5, 1).map(decode("impor", _)).filter(decode(_, bytes).contains("main")).toList.headOption.map(decode(_, bytes))
println(res.getOrElse("Solution not found"))
@mkulak
mkulak / Stars.scala
Created June 12, 2012 06:45
Darkus June FP contest solution
import io.Source
object Stars {
type Point = Array[Int]
type Tree = Array[Array[Array[List[Point]]]]
def main(args:Array[String]) {
val start:Long = System.nanoTime()
val coords = Source.fromFile("stars.dat").getLines().map(s => s.split("\\s+").map(_.toInt)).toArray
var minDistance = Long.MaxValue
@mkulak
mkulak / gist:3099255
Created July 12, 2012 16:48
Sad scala behavior
val map:Map[String, Object] = Map("word" -> "complex")
val str:String = "scala is so complex"
val index:Int = str indexOf map("word")
println(""""%s" indexOf "%s" = %d""".format(str, map("word"), index))
------------------------------
"scala is so complex" indexOf "complex" = -1
@mkulak
mkulak / p94.clj
Last active November 1, 2015 20:47
Game of life in Clojure and Kotlin (http://www.4clojure.com/problem/94)
(defn solve [input]
(let [neighbours (fn [[y x]]
(filter #(not= % [y x]) (for [i (range -1 2) j (range -1 2)] [(+ y i) (+ x j)])))
isLive (fn [[y x]]
(and (>= y 0) (< y (count input)) (>= x 0) (< x (count (input y)))
(not= (nth (input y) x) \space)))
shouldLive (fn [p]
(let [liveNeighbours (count (filter isLive (neighbours p)))]
(or (= liveNeighbours 3) (and (isLive p) (= liveNeighbours 2)))))]
(map
@mkulak
mkulak / CalcSumSquare.kt
Created November 3, 2015 22:37
Calculating sum of squares of rectangles, which bottoms are located on one line
data class Rect(val left:Float, val right:Float, val height:Float)
data class Mark(val rect:Rect, val value:Float, val isFinish:Boolean)
fun calcSquare(data:List<Rect>):Float {
if (data.isEmpty()) {
return .0f
}
val marks = arrayListOf<Mark>()
for (rect in data) {
fun luckyTicketsCount():Int {
fun digits(i:Int) = arrayOf(
i % 10,
(i / 10) % 10,
(i / 100) % 10,
(i / 1000) % 10,
(i / 10000) % 10,
(i / 100000) % 10
)
fun isLucky(i:Int):Boolean {
@mkulak
mkulak / KingElectionActorsExample.kt
Last active November 27, 2015 21:14
Implementation of test task for Echo.
package core
import co.paralleluniverse.actors.ActorRef
import co.paralleluniverse.actors.ActorRegistry
import co.paralleluniverse.fibers.Suspendable
import co.paralleluniverse.kotlin.Actor
import co.paralleluniverse.kotlin.register
import co.paralleluniverse.kotlin.spawn
import co.paralleluniverse.strands.Strand
import java.util.*
((d:Seq[(String, Int)]) =>
println(d.map(t => t._1 + " " + t._2).mkString("\n") + "\n\nВиноват " +
(if (d.map(_._1).take(5).contains("Вевелония")) "Ганс Шмуцихь" else "Вольфганг Кунстштюк")))
(Source.fromFile("/home/scrum/data.txt").getLines().map(_.split(":")).flatMap(as => as(1).split(",").map(_.trim)
.map((_, as(0)))).toArray.groupBy(_._1).mapValues(_.length).toSeq
.sortWith((a, b) => if (a._2.compareTo(b._2) == 0) a._1.compareTo(b._1) < 0 else a._2.compareTo(b._2) > 0))
Output:
Россия 10