Skip to content

Instantly share code, notes, and snippets.

@bjmllr
bjmllr / rbpm.rb
Last active September 21, 2017 23:26
metaprogramming atrocity
module Mat
module_function def ch(input, &block)
matcher = Matcher.new
matcher.instance_exec(&block)
matcher.match(input)
end
module_function def cher(&block)
matcher = Matcher.new
matcher.instance_exec(&block)
# a transversal is an array of Int32.
#
# utilities
# print a square
def p(s, depth = 0)
indent = " " * depth
s.each do |line|
@bjmllr
bjmllr / triangle_peg.rb
Last active June 14, 2016 01:06 — forked from bokmann/triangle_peg.rb
An example from "What Computer Scientists Know: Thinking Recursively, Part 2. In this example we are going to explore how we use our newfound knowledge of recursion to search through all the solutions of a simple puzzle using recursive backtracking.
POSSIBLE_MOVES = [[3,4,5], [5,4,3],
[6,7,8], [8,7,6],
[7,8,9], [9,8,7],
[10,11,12], [12,11,10],
[11,12,13], [13,12,11],
[12,13,14], [14,13,12],
[0,1,3], [4,1,0],
[1,3,6], [6,3,1],
[3,6,10], [10,6,3],
[2,4,7], [7,4,2],
$ time crystal build --release triangle_peg.cr && ./triangle_peg
real 0m1.518s
user 0m1.497s
sys 0m0.040s
Solutions for Board[011111111111111]
Solutions for Board[101111111111111]
Solutions for Board[111011111111111]
Solutions for Board[111101111111111]
Benchmark: 2.860000 0.040000 2.900000 ( 2.267569)
@bjmllr
bjmllr / star_wars.cr
Created September 14, 2015 23:13
crystal client lib for swapi.io
require "http/client"
require "json"
class StarWars
BASE_URI = "http://swapi.co/api"
def self.get(uri)
new.get(uri)
end