Skip to content

Instantly share code, notes, and snippets.

@olooney
olooney / game_of_life_pytorch.py
Last active June 28, 2024 12:52
Run Conway's Game of Life on a GPU using PyTorch
import torch
import torch.nn.functional as F
from PIL import Image
# Check CUDA availability
device = "cuda" if torch.cuda.is_available() else "cpu"
game_of_life_kernel = torch.tensor([
[1, 1, 1],
[1, 0, 1],
"""
There are a surprising number of integers where the square root can be written
as the sum of the digits of the original minus 2. Even more if you're willing
to group digits together in order to make the sum work. This program finds all
299 such examples for n < 10e9. Ultimately this works because the square root
of a number tends to have roughly half as many digits as the original number,
so if you break a number in half, you'll already be in the ballpark of the
square root. For example, `sqrt(442,225) = 665 = (442 + 225) - 2`. The minus
two is arbitary - this works for other offsets as well - but offset 2 gets
you six matches for n <= 289 so it looks impressive right out of the gate.
@olooney
olooney / quine_clock_explained.html
Last active January 19, 2025 12:04
Quine Clock Explained
<script src="quine_clock_explained.js"></script>
<style>
body {
background-color: black;
color: #FFF;
padding: 9vw;
font-size: 20px;
}
</style>
@olooney
olooney / zener.py
Created May 29, 2024 11:34
Creates test images with a grid of simple, Zener-card-like icons
import sys
import csv
import random
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
import numpy as np
# Constants
COLORS = ['red', 'blue', 'green', 'orange', 'purple']
SHAPES = ['circle', 'square', 'triangle', 'plus', 'diamond']
@olooney
olooney / update_known_hosts.bash
Created March 23, 2020 16:53
Shell script to update the .ssh/known_hosts file for all users on a given machine when the host key of a target machine has changed
TARGET_HOST=prometheus
# for each user with a home directory
for USER in `ls /home`; do
# if the user has a known_hosts files
echo "checking /home/$USER/.ssh/known_hosts..."
if [ -f /home/$USER/.ssh/known_hosts ]; then
# remove the outdated key
@olooney
olooney / api.oranlooney.com
Last active November 20, 2019 21:48
Nginx site configuration for a secure REST API reverse proxy served on HTTPS
server {
server_name api.oranlooney.com;
listen 80;
# additional security
server_tokens off;
add_header X-Frame-Options "DENY";
add_header X-Content-Type-Options nosniff;
# performance
from scipy.optimize import linear_sum_assignment
import numpy as np
def maximize_trace(X):
"""
Maximize the trace of a square matrix using only row and
column permutations. Furthermore, sort the trace
in desending order so that largest value ends
up the upper left and the smallest in the lower right.
@olooney
olooney / parallel_steps.py
Created October 25, 2019 00:20
Python parallel worker threads with back-pressure
import os
import sys
from time import sleep
from threading import Thread
from queue import Queue
import codecs
def hard_work(x):
for n in range(100001):
x = codecs.encode(x, 'rot_13')
from typing import NamedTuple, Any, Optional
class Node(NamedTuple):
"""A single Node in a binary tree."""
value: Any
left: Node
right: Node
def count(node: Optional[Node]) -> int:
"""Count the total number of nodes in a tree rooted at this node."""
X = np.hstack([np.ones(shape=(23, 1)), np.random.normal(size=(23, 2))])
Theta = np.array([0.5, +0.1, -0.2]).reshape( (3, 1) )
Y = X @ Theta + np.random.normal(0, 0.1, size=(23, 1))
Theta_hat = np.linalg.inv(X.T @ X) @ X.T @ Y
Theta_hat