Skip to content

Instantly share code, notes, and snippets.

/A0A0.rb Secret

Created January 24, 2013 01:45
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 anonymous/b3c644db0865f153bf1c to your computer and use it in GitHub Desktop.
Save anonymous/b3c644db0865f153bf1c to your computer and use it in GitHub Desktop.
class A0A0
OPS = Hash[*%w[S + D - M * L <=>]]
def initialize source
@program = *source.lines
@line = @program.index { |p| p['>'] } || 0
@program.map! { |p| p.scan /\w-?\d+/ }
end
def debug
@program.each_with_index do |p, i|
puts "%c%d\t%s" % ['* '[i <=> @line], i, (p || []) * ' ']
end
puts '-' * 20
end
def line
@program[@line]
end
def last modify
begin
line[-1] = line[-1].sub(/-?\d+/) { |o| modify[o.to_i] }
rescue NoMethodError
raise "No last number to modify."
end
end
def run
until line.empty?
debug if $DEBUG
insn = line.shift
arg = insn[1..-1].to_i
case insn
when /A/ then (@program[@line + arg] ||= []).concat line
when /C/ then @program[@line + arg] = []
when /G/ then @line += arg
when /O/ then print arg
when /P/ then print (arg % 256).chr
when /I/ then last -> _ { STDIN.getc.send %w[to_i ord][arg] }
when /([SDML])/ then last -> o { o.send OPS[$1], arg }
end
@line += 1 unless insn['G']
break unless line
end
end
end
$DEBUG = true
A0A0.new(ARGF).run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment