Skip to content

Instantly share code, notes, and snippets.

@nrdmn
nrdmn / colors.sed
Created August 11, 2019 20:52
ANSI colors to IRC colors
s/\x1b\[0m/\x0f/g
s/\x1b\[1m/\x02/g
s/\x1b\[30m/\x031/g
s/\x1b\[31m/\x035/g
s/\x1b\[32m/\x033/g
s/\x1b\[33m/\x037/g
s/\x1b\[34m/\x032/g
s/\x1b\[35m/\x036/g
s/\x1b\[36m/\x0310/g
s/\x1b\[37m/\x0315/g
@nrdmn
nrdmn / port.zig
Created May 30, 2019 16:38
Access x86 IO ports in Zig
pub inline fn out(port: u16, value: var) void {
const TypeId = @import("builtin").TypeId;
const maxInt = @import("std").math.maxInt;
const value_ = switch (@typeId(@typeOf(value))) {
TypeId.ComptimeInt => switch (value) {
0...maxInt(u8) => @intCast(u8, value),
maxInt(u8) + 1...maxInt(u16) => @intCast(u16, value),
maxInt(u16) + 1...maxInt(u32) => @intCast(u32, value),
else => @compileError("port out of range"),
@nrdmn
nrdmn / triangles.hs
Last active May 24, 2019 14:27
pythagorean triples
triangles = [(a,b,c) | a<-[1..], b<-[1..a-1], c<-[a..a+b], a^2+b^2==c^2]
main = putStr $ unlines $ map show triangles
@nrdmn
nrdmn / mandelbrot.hs
Created April 29, 2019 14:24
mandelbrot.hs
import Data.Complex
import Data.List
import Control.Parallel.Strategies
mandelbrot c = c : [z^2 + c | z <- mandelbrot c]
color (a,b,c,d) x y
| len < a = ' '
| len < b = '.'
| len < c = ':'
import qualified Data.HashMap.Strict as M
import qualified Data.ByteString.Char8 as B
import Data.List (sortOn)
import Data.List.Split (chunksOf)
import Control.Parallel.Strategies (parMap, rdeepseq)
main = do
words <- B.words <$> B.getContents
let result = sortOn snd $ M.toList $ foldl (M.unionWith (+)) M.empty $ parMap rdeepseq (foldl (\acc x -> M.insertWith (+) x (1 :: Integer) acc) M.empty) (chunksOf 50000 words)
B.putStr . B.concat $ result >>= (\(a,b) -> [B.pack $ show b, B.singleton '\t', a, B.singleton '\n'])
@nrdmn
nrdmn / gist:9bed16155eabe57cfe665173f45a4d2b
Last active July 29, 2024 23:10
NetBSD Pi Hole Howto
/etc/localtime:
ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime
/etc/rc.conf:
inetd=NO
postfix=NO
named=YES
* {
animation-name: colors;
animation-duration: 4s;
animation-iteration-count: infinite;
}
@keyframes colors {
0% { text-shadow: 0 0 1pt hsl(0, 100%, 50%); }
17% { text-shadow: 0 0 1pt hsl(60, 100%, 50%); }
33% { text-shadow: 0 0 1pt hsl(120, 100%, 50%); }
@nrdmn
nrdmn / primes.hs
Last active September 15, 2019 13:17
import System.Environment (getArgs)
import System.IO (hPutStrLn, stderr)
import Data.List (genericTake)
import Text.Read (readMaybe)
-- sqrt possibly inaccurate for large primes.
primes = 2 : [ n | n<-[3,5..], all (\x -> n `mod` x /= 0) $ takeWhile (<= (floor $ sqrt $ fromIntegral n)) primes]
-- guaranteed to be correct for all primes, but much slower:
-- primes = 2 : [ n | n<-[3,5..], all (\x -> n `mod` x /= 0) $ takeWhile (<= (quot n 2)) primes]
-- for all n out of [1..] there's a prime p so that n < p < 2*n (Bertrand's postulate)
@nrdmn
nrdmn / functional.c
Created February 21, 2019 17:37
functional C with lambdas and CPS
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdlib.h>
#define FN void __attribute__((noreturn))
#define LAMBDA(c_) ({ FN _ c_ _;})
@nrdmn
nrdmn / sigtermme.c
Created January 26, 2019 12:27
SIGTERM me!
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
char *name;
void term(int signum)
{