This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rgb(r, g, b)16+(r.to_i*36)+(g.to_i*6)+b.to_i end | |
def colorize(s, fg, bg = nil) "#{fg ? "\x1b[38;5;#{fg}m" : ''}#{bg ? "\x1b[48;5;#{bg}m" : ''}#{s}\x1b[0m" end | |
def rainbow(n)f,w,o=5.0/n,3,3; (0...n).map{|i| rgb(Math.sin(f*i+0)*o+w, Math.sin(f*i+2)*o+w, Math.sin(f*i+4)*o+w)} end | |
def display(board, goal=2048) | |
@palette ||= rainbow(Math.log2(goal)) | |
puts board.map{|r| r.map{|c| colorize((c==0 ? ?. : c).to_s.center(6), @palette[Math.log2(c+1)])}.join}.join("\n\n") | |
end | |
def compress(board, direction) | |
t=->(a){z=0;a.reduce([]){|s,e| z+=e==s[-1]? (s[-1]+=e;1): e>0? (s<<e;0): 1;s}+Array.new(z,0)} # trivial case: 1D array to the left |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##################################################################################### | |
# | |
# Peer base | |
# | |
##################################################################################### | |
require 'socket' | |
require 'thread' | |
require 'ipaddr' | |
require 'json' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'webrick' | |
require 'cgi' | |
class Busker | |
def initialize(opts={}, &block) | |
@routes = {} | |
(block.arity < 1 ? instance_eval(&block) : block.call(self)) if block_given? | |
opts[:Port] ||= opts.delete(:port) || 8080 | |
opts[:DocumentRoot] ||= opts.delete(:document_root) || File.expand_path('./') | |
@server = WEBrick::HTTPServer.new(opts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'webrick' | |
require 'cgi' | |
require 'erb' | |
require 'em-websocket' | |
module EventMachine | |
module WebSocket | |
class Connection < EventMachine::Connection | |
def remote_ip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest/sha2' | |
require 'securerandom' | |
require 'time' | |
def random_seed | |
Digest::SHA512.hexdigest(SecureRandom.random_bytes(64)) | |
end | |
def timekey(rounds, seed = random_seed) | |
# see also: http://www.gwern.net/Self-decrypting%20files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*tinyMd based on https://github.com/SimonWaldherr/micromarkdown.js*/ | |
var tinyMd = { | |
regexobject: { | |
headline: /^(\#{1,6})([^\#\n]+)$/m, | |
code: /\s\`\`\`\n?([^`]+)\`\`\`/g, | |
hr: /^(?:([\*\-_] ?)+)\1\1$/gm, | |
lists: /^((\s*((\*|\-)|\d(\.|\))) [^\n]+)\n)+/gm, | |
bolditalic: /(?:([\*_~]{1,3}))([^\*_~\n]+[^\*_~\s])\1/g, | |
links: /!?\[([^\]<>]+)\]\(([^ \)<>]+)( "[^\(\)\"]+")?\)/g, | |
reflinks: /\[([^\]]+)\]\[([^\]]+)\]/g, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest/md5' | |
# def color_hash(str, opts={}) | |
# ranges = opts[:ranges] || [(64..190),(64..190),(64..190)] | |
# factors = opts[:factors] || [1.0] * ranges.size | |
# factors = [factors.to_f] * ranges.size unless factors.is_a?(Array) | |
# str.bytes.reduce(ranges.each_with_index.map{|e,i| e.size/(i+1)}){ |s,e| | |
# s.each_with_index.map{|x,i|(x*e)%ranges[i].size+ranges[i].first} | |
# }.each_with_index.map{|e,i| | |
# x = (e*factors[i]).to_i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
require 'oily_png' | |
rescue LoadError | |
STDERR.puts 'Warning: Gem oily_png not found. Using chunky_png instead. This will be slow!' | |
require 'chunky_png' | |
end | |
def load_image(file) | |
ChunkyPNG::Image.from_file(file) | |
rescue ChunkyPNG::SignatureMismatch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$files = preg_grep('/\.jpg$/i', glob('*')); | |
if($_GET['thumb'] && in_array($_GET['thumb'], $files)){ | |
$thumb = exif_thumbnail($_GET['thumb'], $width, $height, $type); | |
if($thumb !== false){ // show embedded thumbnail | |
header('Content-type: '.image_type_to_mime_type($type)); | |
echo $thumb; | |
}elseif(false){ | |
// TODO: create thumbnail on the fly with imagemagick if installed | |
}else{ // fallback: show original image |
OlderNewer