Skip to content

Instantly share code, notes, and snippets.

View jkramer's full-sized avatar

Jonas Kramer jkramer

View GitHub Profile
@jkramer
jkramer / ö.p6
Created January 29, 2018 11:18
Ö
#!/usr/bin/env perl6
$*IN.slurp.&talk;
sub talk(Str $_) {
my @ös = |('Ö','ö') xx *;
for .lines {
for .comb.produce(&[~]) {
print "\r", @ös.shift, ' ', $_;
@jkramer
jkramer / Log::Dispatch.pm6
Last active September 26, 2016 13:42
Log::Dispatch for Perl 6
class Log::Dispatch {
enum Level <DEBUG INFO NOTICE WARNING ERROR CRITICAL ALERT EMERGENCY>;
class Output {
has Level $.level = DEBUG;
method format(Level $level, DateTime $ts, @args) {
$ts ~ ' [' ~ $level.lc ~ '] ' ~ @args>>.gist.join(' ')
}
@jkramer
jkramer / blurgh.p6
Created August 3, 2016 21:58
Blurgh Example
use Blurgh;
class Blog::Post does Blurgh::Route[:('post')] {
state %posts;
state $next-post-id = 1;
# Matches "GET /post/123" (only if integer exists in %posts)
multi method get(Int:D $id where { %posts{$_}:exists }) {
$.render(:text(%posts{$id}));
}
@jkramer
jkramer / initphase.hs
Created January 12, 2016 12:58
CodinGame - APU: Init Phase
import Control.Monad
import Data.List
main = do
[ _, height ] <- replicateM 2 (fmap read getLine)
cells <- fmap concat (forM [0 .. height - 1] (\ y -> fmap (parseRow y) getLine))
forM cells (putStrLn . findNext cells)
where
parseRow y cells = map ((,) y . fst) $ filter ((==) '0' . snd) $ zip [0..] cells
@jkramer
jkramer / horse.hs
Created January 12, 2016 11:55
CodinGame - Horse Race
import System.IO
import Control.Monad
import Data.List
main = do
hSetBuffering stdout NoBuffering -- DO NOT REMOVE
count <- fmap read getLine
strengths <- fmap sort (replicateM count (fmap read getLine))
@jkramer
jkramer / virus.hs
Last active January 12, 2016 11:20
CodinGame - Virus
import Data.List
import Data.Ord
import System.IO
import Control.Monad
type Node = Int
type Gateway = Node
type Link = (Node, Node)
type Path = [Link]
@jkramer
jkramer / chasm.hs
Created January 11, 2016 15:26
CodinGame - Chasm
import System.IO
import Control.Monad
main = do
hSetBuffering stdout NoBuffering -- DO NOT REMOVE
[road, gap, platform] <- replicateM 3 (fmap read getLine)
forever $ do
[speed, x] <- replicateM 2 (fmap read getLine)
@jkramer
jkramer / temps.hs
Last active January 11, 2016 14:06
CodinGame - Temperatures
import System.IO
import Data.List
import Data.Ord
main = do
hSetBuffering stdout NoBuffering
n <- fmap read getLine
if n == 0
then print 0
else getLine >>= print . head . sortOn abs . reverse . sort . map read . words
@jkramer
jkramer / thor.hs
Created January 11, 2016 14:04
CodinGame - Thor
import System.IO
main = do
hSetBuffering stdout NoBuffering -- DO NOT REMOVE
[lightx, lighty, initialtx, initialty] <- fmap (map read . words) getLine
mapM_ (\ x -> getLine >> putStrLn x) (path initialtx initialty lightx lighty)
path x y lx ly =
zipWith (++) (direction "S" "N" (y - ly)) (direction "E" "W" (x - lx))
where
@jkramer
jkramer / mime.hs
Created January 11, 2016 14:02
CodinGame - MIME Type
import Control.Monad
import Data.Char
import Data.Maybe
import Data.List
main = do
[n, q] <- replicateM 2 (fmap read getLine)
types <- replicateM n (fmap ((\ [x, y] -> (map toLower x, y)) . words) getLine)
replicateM q $ do
ext <- fmap (extension . map toLower) getLine