This file contains hidden or 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
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 |
This file contains hidden or 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
#!/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?' |
This file contains hidden or 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
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} |
This file contains hidden or 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
#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 |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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 |