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 / 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 / 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 / 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 / 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 / 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 / Makefile
Last active June 6, 2021 22:03
Simple Makefile
TARGET_EXEC := your_program
BUILD_DIR := bin
SRC_DIRS := src
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
CC := gcc
@jeremy-rifkin
jeremy-rifkin / memory_order_roulette.hpp
Created July 1, 2021 21:02
This can only end well
#include <atomic>
#include <stdlib.h>
constexpr std::memory_order orders[] = {
std::memory_order_relaxed,
std::memory_order_consume,
std::memory_order_acquire,
std::memory_order_release,
std::memory_order_acq_rel,
std::memory_order_seq_cst
@jeremy-rifkin
jeremy-rifkin / 1k_errors.cpp
Created July 9, 2021 02:16
Shortest known program to cause over 1,000 lines of error messages in gcc.
// C++ error messages are notorious.
// Some folks and I worked on a golf challenge: Shortest program to produce more
// than a thousand lines of error messages in gcc.
// Constraints: gcc >= 8, no -fmessage-length
// Fueled by the decltype(decltype) bug discovered by Jonathan Wakely (detailed
// at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92105), this is the current record
// 32 characters:
#define d decltype
@jeremy-rifkin
jeremy-rifkin / pi.c
Created July 21, 2021 21:03
A program to print pi which has *absolutely no* UB whatsoever
#include <stdio.h>
#include <math.h>
char s[100000];
prime(p,r,i,m,e) {
for(e=sizeof(s),r=e/2/2/2/2/2/2/2/2/2/2/2/2/2/2/2/2*2; r<e>>1; r++)
for(i=r+r-r+r; i<e; i+=r)
s[+i]++;
for(m=(m=2,p=p^m,m=p^m,p=p^m,m); m+m+m+m; p++)
!s[p]&&m--;
return--p;
@jeremy-rifkin
jeremy-rifkin / add.cpp
Last active August 8, 2021 02:33
Very simple idiomatic C++ program to add two numbers together
#include <cstdint>
#include <functional>
#include <memory>
#include <tuple>
#include <utility>
[[nodiscard]] decltype(std) add(const std::unique_ptr<std::tuple<std::int32_t and, std::int32_t and>> and t) noexcept {
return std::plus{}(std::min(std::forward<std::int32_t>(std::get<0>(*t)), std::forward<std::int32_t>(std::get<1>(*t))),
std::max(std::forward<std::int32_t>(std::get<0>(*t)), std::forward<std::int32_t>(std::get<1>(*t))));
}