Created
May 6, 2017 18:51
-
-
Save ConorOBrien-Foxx/771c36bf38a714c98816c528750d7a7b to your computer and use it in GitHub Desktop.
Extensible metagolfer
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
" | |
double: <<> | |
parity swap: ::^~<~^ | |
" | |
class Transformation | |
def initialize(sym, inv) | |
@sym = sym | |
@inv = inv | |
end | |
attr_reader :sym, :inv | |
def [](other) | |
@inv[other] | |
end | |
end | |
class Golfer | |
def initialize(trans, cons = {}) | |
@trans = trans | |
@pool = cons | |
end | |
attr_reader :pool, :trans | |
def step | |
@pool.clone.each { |c, key| | |
@trans.each { |t| | |
r = t[c] | |
unless @pool.has_key? r | |
k = key + t.sym | |
@pool[r] = k | |
end | |
} | |
} | |
end | |
def [](n) | |
step until @pool.has_key? n | |
@pool[n] | |
end | |
end | |
# ~ is a self-inverse | |
bit_neg = Transformation.new "~", -> x { ~x } | |
bit_shft_left = Transformation.new ">", -> x { x / 2 } | |
bit_shft_right = Transformation.new "<", -> x { x * 2 } | |
g = Golfer.new( | |
[bit_neg, bit_shft_left, bit_shft_right], | |
{ 0 => "" } | |
) | |
n = 0 | |
while true | |
puts "#{n} -> #{g[n]}" | |
n += 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment