Skip to content

Instantly share code, notes, and snippets.

View jeremy-rifkin's full-sized avatar
🐉

Jeremy Rifkin jeremy-rifkin

🐉
View GitHub Profile
@jeremy-rifkin
jeremy-rifkin / malloc.hpp
Last active April 6, 2022 12:37
High performance malloc implementation
uintptr_t base;
const uintptr_t height = 100000000;
std::mt19937 rng;
[[gnu::constructor]] void init_malloc() {
base = (uintptr_t) sbrk(height);
}
void* malloc(size_t) { // parameter ignored, we don't need it
return (void*)(base + rng() % height); // odds of any collision is like, low
}
void free(void*) {} // no-op
@jeremy-rifkin
jeremy-rifkin / enumerate.h
Created March 26, 2021 01:30
C++ python-like enumerate
template<typename T> class enumerate {
T& iteratable;
public:
enumerate(T& iteratable) : iteratable(iteratable) {}
template<typename I> class iterator {
I it;
int i = 0;
public:
iterator(I it) : it(it) {}
iterator operator++() { let i = *this; operator++(0); return i; }
@jeremy-rifkin
jeremy-rifkin / range.h
Last active March 26, 2021 01:06
C++ range iterator
#include <cmath>
#include <cassert>
#include <type_traits>
template<typename T> int sign(T t) {
return t < 0 ? -1 : 1;
}
template<typename T> class range {
typedef typename std::make_signed<T>::type ST;
@jeremy-rifkin
jeremy-rifkin / brainf_interpreter.c
Created February 11, 2021 03:20
Code golfed brainf interpreter
// brainf interpreter code golf
// collaboration by some folks on discord and myself
#include <stdio.h>
#define r(c) ;i++;goto w;case c:
i;t[99];s;p=1024;char b[2048];main(int j,char**a) {
fread(b,1,p,fopen(a[1],"r"));
w: switch(b[i]) {
r(45) b[p]--
r(43) b[p]++
r(60) p--
@jeremy-rifkin
jeremy-rifkin / mandelbrot.c
Last active February 8, 2021 23:47
137 char mandelbrot code golf
// 137 char mandelbrot code golf
// collaboration by myself and others on a programming discord server
y=40,x;main(i){for(;y--;puts(""))for(x=170;x--;printf(L" .'-+=%@#"+i/12))for(typeof(0.i)z=i=0;cabs(z)<2&i++<99;z=z*z-x/50.+y/20.i+1+1i);}
// 130 char, lower-detail:
y=40,x;main(i){for(;--y;puts(""))for(x=170;x--;printf(L" *"+i/99))for(typeof(0.i)z=i=0;cabs(z)<2&i++<98;z=z*z-x/50.+y/20.i+1+1i);}
// compiler: gcc, may need -lm
@jeremy-rifkin
jeremy-rifkin / duplicate_file_resolution.py
Created December 3, 2020 03:22
7zip has trouble compressing a directory on windows if it contains two filenames which differ only in capitalization... This script will find problematic file pairs and delete conflicts.
import os
import sys
def main():
filedict = {}
for path, dirs, files in os.walk(sys.argv[1]):
for f in files:
full_path = os.path.join(path, f)
l = full_path.lower()
if l not in filedict:
@jeremy-rifkin
jeremy-rifkin / playtime.py
Created November 26, 2020 01:25
Script to parse minecraft server logs and add up users' playtime.
#!/bin/python
# Script to parse minecraft server logs and add up users' playtime.
# Usage: python3 playtime.py path/to/logs
#
# Rifkin 2020
import datetime
import gzip
import os
@jeremy-rifkin
jeremy-rifkin / ifunny_filter.py
Last active June 28, 2023 11:58
A script to detect photos with "ifunny" watermarks and filter them from other photos.
# This is a program to filter photos with ifunny watermarks.
# I wrote this script for personal use to separate saved ifunny memes from other photos on my phone.
# The script is fully parallelized and is able to make good use of technologies like Intel
# hyperthreading because substantial program time is spent in IO. Bottleneck will be IO.
#
# Watermark false positive rate appears to be < 1/500
# Watermark false negative rate appears to be ~ 1/500
# False positive / negative rate checked with manual spot check sample size = 500.
#
# Rifkin 2020
@jeremy-rifkin
jeremy-rifkin / file_sample.py
Created November 25, 2020 19:10
Quick utility to pick n random files from a directory
#!/usr/bin/python
import os
import shutil
import sys
import random
def pick_random(src, dest, N):
all_files = []
for path, dirs, files in os.walk(src):
for f in files:
@jeremy-rifkin
jeremy-rifkin / non-recursive-ackermann.py
Created November 10, 2020 08:26
To show a friend (who confused recursion and primative recursion) that the ackermann function can be computed without recursion.
# definition recursive ackermann
def A(m, n):
if m == 0:
return n + 1
elif n == 0:
return A(m - 1, 1)
else:
return A(m - 1, A(m, n - 1))
# non-recursive ackermann