Skip to content

Instantly share code, notes, and snippets.

fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
Error::Transient(ref e) => Some(e),
Error::Fatal(ref e) => Some(e),
}
}
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
&Error::Transient(ref e) => Some(e),
@depp
depp / heightmap_smooth.py
Last active September 17, 2019 14:35
How to Create a Smooth Heightmap
# https://www.reddit.com/r/proceduralgeneration/comments/d5gde4/heightmap_with_strict_neighbour_cell_constraints/
import numpy as np
import scipy.ndimage as ndimage
def generate(w, h, d):
return np.random.randint(0, d, (w, h))
def smooth(arr):
for z in range(np.min(arr), np.max(arr) + 1):
# Set of all tiles at height Z.
@depp
depp / find_merge_conflicts.py
Created April 23, 2019 19:11
Find Git merge conflicts
# https://stackoverflow.com/questions/55817243/does-git-keep-a-record-of-past-merge-conflicts
import subprocess
import sys
def list_merges():
proc = subprocess.run(
['git', 'rev-list', '--parents', '--min-parents=2', '--all'],
stdout=subprocess.PIPE,
check=True,
encoding='ASCII',
@depp
depp / hello32.asm
Created April 9, 2019 16:10
32-bit Hello World for DOS with DOS/32A
;; 32-bit Hello World
;; This creates a Linear Executable which can be run with DOS/32A.
;; See: http://dos32a.narechk.net/index_en.html
;;
;; To compile,
;; nasm -o hello32.exe -f bin hello32.asm
;; To run,
;; dos32a hello32
;; Screenshot: https://imgur.com/EiaU2py
bits 32
@depp
depp / scrape.py
Created February 26, 2019 15:56
Scraping with Python
# https://stackoverflow.com/questions/54876010
from bs4 import BeautifulSoup
import json
import requests
import sys
from urllib.parse import urljoin
def get_list(uri):
"""Scan the manga list for links to manga."""
response = requests.get(uri)
@depp
depp / pipe.py
Created August 9, 2018 14:12
Simple command piping in Python
# https://stackoverflow.com/questions/51756938/how-to-make-a-generic-method-in-python-to-execute-multiple-piped-shell-commands
import shlex
import subprocess
def run_pipe(cmds):
pipe = subprocess.DEVNULL
procs = []
for cmd in cmds:
proc = subprocess.Popen(cmd, stdin=pipe, stdout=subprocess.PIPE)
procs.append(proc)
@depp
depp / test.hs
Created March 5, 2018 05:51
Haskell Benchmark
{-# LANGUAGE BangPatterns #-}
-- https://stackoverflow.com/questions/49080119/why-is-statet-faster-in-this-example
module Main where
import Control.Applicative
import Control.Monad.State.Strict
import Criterion.Main
import Data.IORef
import System.Environment
@depp
depp / add.cpp
Created February 15, 2018 16:48
Bignum addition in inline assembly
// See: https://stackoverflow.com/a/48812277/82294
template <int N>
void add(unsigned long long *dest, unsigned long long *src) {
__asm__(
"movq (%1), %%rax"
"\n\taddq %%rax, (%0)"
"\n.local add_offset"
"\n.set add_offset,0"
"\n.rept %P2"
@depp
depp / problem.cpp
Last active December 21, 2021 19:04
A Faster Solution
// Faster solution for:
// http://www.boyter.org/2017/03/golang-solution-faster-equivalent-java-solution/
// With threading.
// g++ -std=c++11 -Wall -Wextra -O3 -pthread
// On my computer (i5-6600K 3.50 GHz 4 cores), takes about ~160 ms after the CPU
// has warmed up, or ~80 ms if the CPU is cold (due to Turbo Boost).
// How it works: Start by generating a list of losing states -- states where the
// game can end in one turn. Generate a new list of states by running the game
@depp
depp / test3.c
Created May 1, 2015 06:37
Data race
~ $ cc -fPIC -pie -fsanitize=thread -pthread test3.c -Wall -Wextra -g
~ $ ./a.out 5
i am thread 1 .created a new thread(132118272) in the itaration 0
i am thread 1 .created a new thread(123725568) in the itaration 1
==================
WARNING: ThreadSanitizer: data race (pid=20232)
Read of size 4 at 0x7ffcdeefaadc by thread T1:
#0 thread /home/depp/test3.c:10 (a.out+0x000000000c14)
#1 <null> <null> (libtsan.so.0+0x000000023519)