Skip to content

Instantly share code, notes, and snippets.

View sirupsen's full-sized avatar
🐡

Simon Eskildsen sirupsen

🐡
View GitHub Profile
@sirupsen
sirupsen / README.md
Created September 15, 2013 16:10
Solution to an algorithmic problem using LCA to compute the distance between two nodes in a tree in logarithmic time.

Solution to the Flea Circus problem. It uses a range minimum query segment tree to compute the lowest common ancestor between two nodes in the tree, thus the distance from one node in a tree to another can be computed in O(log n) time, and the tree can be built in O(log(n) n) time. This allows the entire problem to be solved in O(log(n) n) time.

@sirupsen
sirupsen / gist:6575106
Created September 15, 2013 23:12
RSA implementation in C++ with an accompanying naive cracker.
#include<iostream>
#include<gmpxx.h>
#include<gmp.h>
#include<vector>
#include<string>
#include<cstdio>
using namespace std;
void set_prime(mpz_t rop)
{
@sirupsen
sirupsen / segment_tree.go
Created September 15, 2013 23:26
Implementation of a general segment tree in Go.
package segment_tree
import "fmt"
const sizeMultiplier int = 3
type Comparator func(int, int) int
type Node struct {
set bool
@sirupsen
sirupsen / gist:7104058
Last active December 26, 2015 05:58
Vendor all the things with Bundler. This is ~/.bundler/config.
---
BUNDLE_PATH: ./vendor/bundle
BUNDLE_DISABLE_SHARED_GEMS: '1'
BUNDLE_JOBS: 3
@sirupsen
sirupsen / gist:7111270
Last active December 26, 2015 06:58
Standard I/O examples in a few languages for Contestrus. You get two numbers from standard input and have to output the sum to standard output.
var fs = require("fs");
input_lines = fs.readFileSync('/dev/stdin').toString().split('\n').map(function(s) {
return s.trim();
});
nums = input_lines[0].split(' ').map(Number)
console.log(nums[0] + nums[1])
@sirupsen
sirupsen / omg.c
Created February 14, 2014 13:48
Virtual memory to physical memory.
$ ps -o rss,command | grep ./omg | grep -v grep
488 ./omg
$ ps -o rss,command | grep ./omg | grep -v grep
10268 ./omg
@sirupsen
sirupsen / gist:9961145
Created April 3, 2014 19:29
Ever-evolving light wrapper on Net::HTTP instead of always reaching for a cheatsheet.
require 'net/http'
require 'uri'
require 'json'
class ProperHTTP
def self.get(uri)
handle_response Net::HTTP.get_response(URI.parse(uri))
end
def self.post(uri, payload)
$ sudo apt-get install maven
The following extra packages will be installed:
ant ant-optional aspectj bsh fop java-wrappers junit junit4 libaether-java libaopalliance-java libapache-pom-java libasm3-java
libaspectj-java libasync-http-client-java libatinject-jsr330-api-java libavalon-framework-java libbatik-java libbsf-java libbsh-java
libcdi-api-java libcglib-java libclassworlds-java libcommons-beanutils-java libcommons-cli-java libcommons-codec-java
libcommons-collections3-java libcommons-configuration-java libcommons-digester-java libcommons-httpclient-java libcommons-io-java
libcommons-jexl2-java libcommons-jxpath-java libcommons-lang-java libcommons-logging-java libcommons-net2-java libcommons-parent-java
libcommons-vfs-java libdom4j-java libdoxia-java libeasymock-java libfop-java libganymed-ssh2-java libgeronimo-interceptor-3.0-spec-java
libgeronimo-jpa-2.0-spec-java libgeronimo-osgi-support-java libguava-java libguice-java libhamcrest-java libhttpclient-java
libhttpcore-java libitext1-ja
@sirupsen
sirupsen / gist:7ebd15c7e2e7469ce0e1
Created August 1, 2014 15:02
BTRFS statsd monitor script
#!/bin/bash
TOTAL=$(sudo btrfs filesystem show /dev/mapper/lvroot-u | grep -oP '(?<=size )\d+\.\d+')
USED=$(sudo btrfs filesystem show /dev/mapper/lvroot-u | grep -oP '(?<=used )\d+\.\d+' | head -n1)
ALLOCATED=$(sudo btrfs filesystem show /dev/mapper/lvroot-u | grep -oP '(?<=used )\d+\.\d+' | tail -n1)
FREE=$(echo "$TOTAL - $USED" | bc)
echo -e -n "system.btrfs.total:$TOTAL|g" > /dev/udp/127.0.0.1/8125
echo -e -n "system.btrfs.used:$USED|g" > /dev/udp/127.0.0.1/8125
echo -e -n "system.btrfs.allocated:$USED|g" > /dev/udp/127.0.0.1/8125
@sirupsen
sirupsen / resiliency.sh
Created September 3, 2014 14:52
Script to set up port redirection and disable it again. The tricky thing here is that even when you disable the port redirect, traffic can still flow through the redirected port for already established socket sessions, but new connections can't be established. This, in addition to removing the redirect, gdbs into the process and closes the file …
#!/bin/bash
if [[ -z $1 ]]; then
echo -e "\x1b[31mMust supply src + dest port"
exit 1
fi
echo -e "\x1b[32mForwarded port $2 --> $1\x1b[33m"
sudo iptables -t nat -I OUTPUT -p tcp -o lo --dport $2 -j REDIRECT --to-ports $1