Skip to content

Instantly share code, notes, and snippets.

View jedisct1's full-sized avatar

Frank Denis jedisct1

View GitHub Profile
@jedisct1
jedisct1 / Escape from Zurg in Ruby.rb
Created April 17, 2011 11:33
My simple "Escape from Zurg" solver
require "pp"
DUDES = [ { name: "Alice", time: 5 },
{ name: "Bob", time: 10 },
{ name: "Candace", time: 20 },
{ name: "Dave", time: 25 } ]
INITIAL_STATE = { left: [ ], right: (0...DUDES.length).to_a, time_left: 60 }
def backward(state, path)
@jedisct1
jedisct1 / gist:943764
Created April 27, 2011 05:33
MongoDB puzzle
n = rand(10) + 2
list = (1..n).to_a.shuffle
list[1 + rand(list.length - 1)] = list.first
puts list.join(", ")
a = n * n.succ / 2
b = n * n.succ * (2 * n).succ / 6
a2 = list.inject(:+)
b2 = list.inject(0) { |acc, x| acc + x * x }
missing = ((b2 - b) / (a2 - a) - a2 + a) / 2
duplicate = a2 - a + missing
@jedisct1
jedisct1 / causes-puzzle.erl
Created May 4, 2011 16:07
My modest attempt at solving the causes.com puzzle
-module(causes).
-export([causes/1]).
leven1(Word, Word) -> not_friends;
leven1(Left, Right) when length(Left) == length(Right) ->
leven1_subst(Left, Right, friends);
leven1(Left, Right) -> leven1_insert(Left, Right).
leven1_subst([], [], _Acc) -> friends;
@jedisct1
jedisct1 / twitter-favorites.rb
Created May 17, 2011 20:55
Dump your Twitter favorites to a text file
#! /usr/bin/env ruby
# Just a small script I made to dump my favorite tweets to a text file.
# Because agrep in a text file is a super powerful and mega fast search engine :)
# Change USER and periodically run $ ruby twitter-favorites.rb >> favorites.txt
require 'twitter'
USER = '<your twitter user name>'
SINCE_ID_FILE = "#{ENV['HOME']}/.favorites_since_id"
@jedisct1
jedisct1 / gist:982921
Created May 20, 2011 13:48
ftp.ibiblio.org is broken
$ ftp ftp.ibiblio.org
Connected to ftp.ibiblio.org.
220 ProFTPD Server
Name (ftp.ibiblio.org:j): ftp
331 Anonymous login ok, send your complete email address as your password
Password:
230-
Welcome to ftp.ibiblio.org, the public ftp server of ibiblio.org. We
hope you find what you're looking for.
@jedisct1
jedisct1 / code of duty.c
Created June 12, 2011 08:54
Code of Duty challenge
/* ---------- Code of duty 1 - Frank DENIS <f@orbus.fr> @jedisct1 ---------- */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#define MIN_ITEM_VALUE 0U
@jedisct1
jedisct1 / realpath.sh
Created July 8, 2011 18:00
realpath() in shell
realpath() {
local file="$1"
local _file
local realpath_file
while _file=$(readlink -- "$_file"); do file="$file"; done
if [ -d "$file" ]; then
realpath_file=$(cd -- "$file" && pwd -P)
else
local dir=$(dirname -- "$file")
local realpath_dir=$(cd -- "$dir" && pwd -P)
@jedisct1
jedisct1 / udpsend.c
Created July 18, 2011 04:36
Stupid UDP sender
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
@jedisct1
jedisct1 / thtest.c
Created July 19, 2011 17:07
stdio streams are *not* thread-safe
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define NTH 100U
#define MXC 100U
#define JUNK "UIACZNEYIAZCBIYAZUBYECIZAUBEIYZUABCEYZAUBEYIAZUBCYAUEBCYZIU" \
"UIACZNEYIAZCBIYAZUBYECIZAUBEIYZUABCEYZAUBEYIAZUBCYAUEBCYZIU" \
"UIACZNEYIAZCBIYAZUBYECIZAUBEIYZUABCEYZAUBEYIAZUBCYAUEBCYZIU" \
@jedisct1
jedisct1 / faster-djb2-hash.c
Created July 23, 2011 16:31
Faster version of the djb2 hash by making gcc recognize it is deterministic
uint32_t djb_hash(const char * const key, size_t keylen)
{
uint32_t j = (uint32_t) 5381U;
const unsigned char *ukey = (const unsigned char *) key;
size_t i = (size_t) 0U;
if (keylen >= (size_t) 8U) {
const size_t keylen_chunk = keylen - 8U;
while (i <= keylen_chunk) {
const unsigned char * const p = ukey + i;
i += (size_t) 8U;