Skip to content

Instantly share code, notes, and snippets.

View nthistle's full-sized avatar

Neil Thistlethwaite nthistle

View GitHub Profile
@nthistle
nthistle / aoc_tools.py
Created December 17, 2023 00:58
the current version of aoc_tools.py I'm using, which just has a bunch of utilities for advent of code
from math import *
from collections import deque, defaultdict, Counter
import itertools
import re
from typing import TypeVar, Generator, Iterable, Tuple, List
import heapq
hpush = heapq.heappush
hpop = heapq.heappop
@nthistle
nthistle / day10_alternate_part2.py
Created December 16, 2023 02:12
alternate way of solving part 2 of aoc 2023 day 10
import sys
sys.setrecursionlimit(1000000)
ans = res = 0
with open("input.txt") as f:
s = f.read().strip()
##s = """FF7FSF7F7F7F7F7F---7
##L|LJ||||||||||||F--J
##FL-7LJLJ||||||LJL-77
##F--JF--7||LJLJIF7FJ-
##L---JF-JLJIIIIFJLJJ7
@nthistle
nthistle / byte_flipping_demo.py
Created November 18, 2021 23:12
Demo for AES-CBC Byte-Flipping
# pip install flask
# pip install pycryptodome
from flask import Flask
from flask import request
from Crypto.Cipher import AES
import os
import base64
import json
import urllib
@nthistle
nthistle / aes_cbc_padding_oracle_decryption.py
Created August 10, 2020 16:26
Reusable code for getting arbitrary decryption with AES-CBC PKCS#7 Padding Oracle
#!/usr/bin/env python3
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import os
class PaddingOracle:
def __init__(self, key=None, iv=None):
if key is None: key = os.urandom(32)
if iv is None: iv = os.urandom(16)
self._key = key
@nthistle
nthistle / stochastic_matrix_similarity.py
Created June 15, 2020 15:29
Some tests with stochastic matrix similarity (by another stochastic matrix)
import numpy as np
def gen_stochastic(size):
m = np.random.random(size)
for i in range(size[0]):
m[i,:] = m[i,:] / np.sum(m[i,:])
return m
def row_sums(m):
return np.array([
@nthistle
nthistle / gaussian_kernel_example.py
Created April 1, 2020 22:30
Example of a Gaussian kernel
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-6, 6, 1000)
def gauss(x,h):
x = x / h
return (1/h) * (1/np.sqrt(2 * np.pi)) * np.exp(-.5 * (x**2))
plt.plot(x, gauss(x, 1), label="h=1")
@nthistle
nthistle / byte_flipping_demo.py
Created February 6, 2020 22:30
Demonstration of Byte Flipping in AES-CBC.
from Crypto.Cipher import AES
from binascii import hexlify
# Arbitrary key and random IV
key = b'sixteen byte key'
iv = b'\xfc\x86I\xd8a\x16\x10\xf9a_4\xb5\xd72\xd7\xeb' # random
# Message is already 32 bytes
msg = b"It's Crypto 2: Electric Boogaloo"
@nthistle
nthistle / minesweeper_discord.py
Created September 12, 2019 22:45
quick script to generate a minesweeper board in "discord format" and paste line by line
import random
import pyautogui
import pyperclip
import time
print("Generating board...")
SIZE = (25, 25)
IDEAL_RATIO = 0.18
NUM_MINES = 115 #int(SIZE[0] * SIZE[1] * IDEAL_RATIO)
@nthistle
nthistle / BC19_LCG.java
Last active January 8, 2019 07:14
Simple LCG for generated pseudorandom numbers in Java for Battlecode 2019. Uses glibc's LCG parameters.
package bc19;
// NOTE: THIS DOES NOT WORK! For some reason, the transpilation does something weird
// to the logic (presumably not treating the values as 32-bit integers). This is NOT
// suitable as a random function, please just use Math.random
public class MyRobot extends BCAbstractRobot {
int x_LCG;
/**
@nthistle
nthistle / z3_example_solution.py
Last active January 6, 2019 21:27
Z3 solution for a problem posted in 'actually good math problems'
from z3 import *
questions = [[Bool('Q%dA' % i), Bool('Q%dB' % i)] for i in range(1,11)]
# Returns expression that evaluates to True only when
# the answers to questions q1 and q2 are the same
def QEq(q1, q2):
return And(questions[q1-1][0] == questions[q2-1][0],
questions[q1-1][1] == questions[q2-1][1])