Skip to content

Instantly share code, notes, and snippets.

@ConorOBrien-Foxx
Created May 6, 2017 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ConorOBrien-Foxx/771c36bf38a714c98816c528750d7a7b to your computer and use it in GitHub Desktop.
Save ConorOBrien-Foxx/771c36bf38a714c98816c528750d7a7b to your computer and use it in GitHub Desktop.
Extensible metagolfer
"
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