Skip to content

Instantly share code, notes, and snippets.

@duncanbeevers
Created January 6, 2010 01:59
Show Gist options
  • Save duncanbeevers/269933 to your computer and use it in GitHub Desktop.
Save duncanbeevers/269933 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'stringio'
class HangMarkus
attr_reader :solutions
FILL_IN_CHARACTERS = (32..176).map { |k| k.chr } - [ '_' ]
def sub_puzzles p
0 == p.count('_') ?
p : FILL_IN_CHARACTERS.map { |c| sub_puzzles(p.sub('_', c)) }
end
def initialize puzzle, expected_output
@solutions = sub_puzzles(puzzle).flatten.select do |s|
begin
$stdout = StringIO.new
eval(s)
rescue Exception
ensure
$stdout.seek(0)
matched = ($stdout.read == expected_output)
end
matched
end
$stdout = STDOUT
end
end
hanger = HangMarkus.new(<<-END_RUBY, "false\n")
a = 1
b = {nil=>a}
c = b.invert
puts _-_
END_RUBY
hanger.solutions.each_with_index do |solution, i|
puts "=== Solution #{i} ====="
puts solution
puts "\n\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment