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 / overflow_game.rb
Created January 27, 2013 21:27
a little logic game in fragments
MAX_CAPACITY = 3
def show(m)
puts "="*12,m.map{|l|' '<<l.map{|e|'*123456789'[e]}.join(' ')}.join("\n"),"="*12
end
def pop(map, x,y)
m = map.map(&:dup)
return m if x < 0 || y < 0 || x >= m[0].size || y >= m.size
if m[y][x] == MAX_CAPACITY
@pachacamac
pachacamac / serious_questions.sh
Last active December 14, 2015 16:09
Ask random serious questions at random times to help you connect with reality once in a while.
#!/bin/bash
# install: copy this script to /opt/serious_questions.sh, "chmod +x" it and add the next line to /etc/rc.local
# nohup /opt/serious_questions.sh > /dev/null &
questions=(
'Are you doing something useful right now?'
'Surfing Reddit like a mindless sheep?'
'Real life ... are you participating?'
'Maybe you should call someone?'
'How about writing something?'
@pachacamac
pachacamac / benchmark.rb
Last active December 17, 2015 17:49
Benchmark some methods and get a summary
class Benchmark
def initialize(opts={})
@default_repetitions = opts[:default_repetitions] || 10
@sum_total, @sum_average, @test_count, @tests = 0, 0, 0, {}
end
def test(repetitions=@default_repetitions, &block)
@test_count += 1
r, t0 = nil, Time.now
repetitions.times{|i| r = yield}
@pachacamac
pachacamac / idletime.c
Created June 1, 2013 10:22
detect if the user is idle for a certain time
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/scrnsaver.h>
// compile: gcc idletime.c -o idletime -lX11 -lXext -lXss
// usage: idletime #shows idle time
// usage: idletime 5 m #waits for 5 minutes idle time
@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 / rtcalarm.rb
Last active January 22, 2016 03:39
Turns Your Laptop into an Alarm Clock While Putting It into Suspend
puts 'Make sure to run this with sudo-rights!' unless Process::UID.eid == 0
parser = begin
require 'chronic'
lambda{|s| Chronic.parse s}
rescue LoadError
STDERR.puts 'Warning: Gem "Chronic" not found. Using Time.parse instead!'
require 'time'
lambda{|s| Time.parse s}
end