Skip to content

Instantly share code, notes, and snippets.

@juliusgeo
juliusgeo / zipy.py
Last active November 19, 2023 18:53
Obfuscated ZIP File Compressor in Python
import sys;itls,colls,binasc,struct,time=[\
__import__(i) for i in("itertools",\
"collections","binascii","struct","time")];\
s=sys.stdin.read();(MOFFSET,MLENGTH,\
FILE_NAME,ZIP_NAME,
idx)=(2047,31,"a.txt"
, "a.zip", 0); \
compressed=[((*\
lo,s[idx]),idx:=\
idx+lo[1])[0] for\
@juliusgeo
juliusgeo / README.md
Created July 10, 2023 23:45
Bits and Pieces: zlib compliant DEFLATE from scratch

Bits and Pieces: zlib compliant DEFLATE from scratch

zlib underlies most zip file decompressors, and DEFLATE is one the binary formats used to store compressed data in a bitstream. The goal of this article is to walk through how my Python DEFLATE compressor implementation works. There are many guides on the internet that describe how to implement each step of DEFLATE, but very few end up producing a bitstream that can actually be parsed by a library like zlib. This article assumes that you roughly know how each step of the DEFLATE algorithm is implemented, but are having trouble with some of the finer points that are often glossed over.

The code can be found in "deflate.py".

@juliusgeo
juliusgeo / main.py
Last active June 21, 2023 10:58
ASCII Eulerian Fluid Simulation in 2kB of Python
import os
timestep=1./60
gravity=-9.81
tg=timestep*gravity
s_x,s_y=10,20
u=v=[[.0 for A in range(s_y)]for A in range(s_x)]
pr=[[.0 for A in range(s_y)]for A in range(s_x)]
st=[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[0,1,1,1,1,0,.1,0,0,.1,.1,.1,.1,.1,.1,.1,0,0,0,0],[0,1,1,1,1,1,.1,.1,.1,.1,.1,.1,.1,.1,.1,0,0,0,0,0],[0,1,1,1,1,1,.1,.1,.1,.1,.1,.1,.1,.1,.1,0,0,0,0,0],[0,.1,0,1,1,1,.1,.1,.1,.1,.1,.1,.1,.1,.1,0,0,0,0,0],[0,.1,0,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,.1,0,0,0,0,0,0],[0,.1,0,.2,.1,.1,.1,0,.1,.1,.1,.1,0,0,0,0,0,0,0,0],[0,.1,0,0,.2,.1,.1,0,.1,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
C=1e3/timestep
loop=[(A,B)for A in range(1,len(u)-1)for B in range(1,len(u[0])-1)]
@juliusgeo
juliusgeo / cursed.md
Last active May 21, 2023 10:53
'Hello, World!' Simplified: Only 24 Lines of Python
h = [
    lambda a, b, c, d: 0,
    lambda a: 0,
    lambda a, b, c, d, e, f, g, h: 0,
    lambda a, b, c, d, e, f, g, h: 0,
    lambda a, b, c, d, e, f, g, h, i, j, k: 0,
    None,
    lambda a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, x, t: 0,
 lambda a, b, c, d, e, f, g, h, i, j, k, l: 0,
@juliusgeo
juliusgeo / bom.py
Last active May 16, 2023 10:07
Constrict Your Ram With Python
from multiprocessing import Process
import psutil
from time import sleep
def ascii_bomb():
return [
"""
.,-:-,,.
@juliusgeo
juliusgeo / typyngtest.py
Last active April 16, 2023 02:35
A pretty nice typing test in 80 lines of Python.
import os,select,tty,sys
from time import time
from functools import partial
# Need this because Unix-esque OSes are different from Windows.
try:
from msvcrt import getch as cf
except ImportError:
tty.setcbreak(sys.stdin.fileno())
cf=partial(sys.stdin.read,1)
@juliusgeo
juliusgeo / mongo_pi.py
Created March 10, 2023 22:16
Pi In MongDB Aggregations
from pymongo import MongoClient
from decimal import localcontext
from math import sqrt, log2
from bson import Decimal128
from bson.decimal128 import create_decimal128_context
coll = MongoClient().db.coll
coll.drop()
def Dec(x):
with localcontext(create_decimal128_context()) as ctx:
return Decimal128(ctx.create_decimal(x))
@juliusgeo
juliusgeo / ioccc_pi.py
Last active March 10, 2023 00:43
IOCCC 1988 Underscore Pi Python Recreation
print(''.join(b:=open(__file__).readlines()[1:]).count("#")/len(b)/4)
#######
#################
#######--------########
######-------------#######
######-----------------######
#####------------------######
######-------------------######
#####--------------------######
#####---------!\---------######
@juliusgeo
juliusgeo / pi_day.py
Last active February 28, 2024 01:59
4 Ways to Calculate Pi
from functools import reduce
from math import factorial, comb, ceil, log2
from decimal import getcontext, Decimal as Dec
def bernoulli(n):
bs = [Dec(1)]
for m in range(1, n+1):
bs.append(1 - sum(comb(m, k)*b / (m - k + 1) for k, b in zip(range(m), bs)))
return abs(bs[-1])

20 million digits of pi in under a minute with Julia

I recently discovered a relatively obscure algorithm for calculating the digits of pi: https://en.wikipedia.org/wiki/Gauss–Legendre_algorithm. Well, at least obscure compared to Chudnovsky's. Wikipedia notes that it is "memory-intensive" but is it really? Let's compare to the MPFR pi function:

function gauss_legendre(prec)
    setprecision(BigFloat, prec, base=10)
    GC.enable(false)