Skip to content

Instantly share code, notes, and snippets.

View pachacamac's full-sized avatar
🌶️
Mmmmhmmmm

Marc pachacamac

🌶️
Mmmmhmmmm
  • Hacker/Founder
  • Germany, Berlin
View GitHub Profile
@pachacamac
pachacamac / 2048.rb
Last active August 29, 2015 13:57
2048 game in concise but not golfed Ruby
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
@pachacamac
pachacamac / beat2beat.rb
Last active August 29, 2015 14:02
multicast ip music player
#####################################################################################
#
# Peer base
#
#####################################################################################
require 'socket'
require 'thread'
require 'ipaddr'
require 'json'
@pachacamac
pachacamac / busker.rb
Last active August 29, 2015 14:03
busker - a minimal, sinatra inspired, webrick based (only using ruby-std lib stuff), web app framework that wants to remain easy and platform independent
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)
@pachacamac
pachacamac / vidup.rb
Created July 10, 2014 12:58
web rtc video chat with ruby backend
require 'webrick'
require 'cgi'
require 'erb'
require 'em-websocket'
module EventMachine
module WebSocket
class Connection < EventMachine::Connection
def remote_ip
@pachacamac
pachacamac / timecapsule.rb
Last active August 29, 2015 14:07
Timecapsule Encryption PoC/WiP
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
@pachacamac
pachacamac / tinyMd.js
Last active August 29, 2015 14:10
tiny javascript markdown parser
/*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,
@pachacamac
pachacamac / color_hash.rb
Created December 4, 2014 00:16
text to colo
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
@pachacamac
pachacamac / scan.rb
Created December 11, 2014 12:08
3d scanner code v1
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
@pachacamac
pachacamac / gallery.php
Created January 12, 2015 12:54
Simple drop in gallery in PHP with thumbnail support