Skip to content

Instantly share code, notes, and snippets.

@canoon
Created May 1, 2012 08:55
Show Gist options
  • Save canoon/2566544 to your computer and use it in GitHub Desktop.
Save canoon/2566544 to your computer and use it in GitHub Desktop.
Unicode HQ9+ Implementation
#!/usr/bin/env ruby
# coding: utf-8
##
# In HQ9+ Hello world programs were unnecessarily long which caused a
# dramatic waste in space. It is estimated that a massive 4 PB of
# storage space worldwide is used to store "Hello, World!" programs a
# further 2 PB of space is used to store "Hello, World" programs. Q9+
# attempts to solve this problem this problem by making the empty file a
# "Hello World" program. As a result of this a standard 2 TB harddrive
# can now store over 2 million Hello World programs. The complete source
# code for the "Hello, World!" program in Q9+ is listed below:
#
#
#
# HQ9+: http://web.archive.org/web/20090602074545/http://www.cliff.biffle.org/esoterica/hq9plus.html
# Q9+'s spec:
#
# - An empty file prints "Hello, world!"
# - Q Prints the entire text of the source code file.
# - 9 Prints the complete canonical lyrics to "99 Bottles of Beer on the Wall"
# - + Increments the accumulator.
#
# Since Q requires the complete source code, you can't process it from stdin.
source_code = ""
while (f = gets) do
source_code << f
end
$accumulator = 0
$bottles = 0
def bottles(n)
if n > 0
$bottles += 1
"#{n} 🍶 of 🍺 on the wall, #{n} 🍶 of 🍺. \nTake one down, pass it around, #{n-1} 🍶 of 🍺 on the wall.\n" + bottles(n-1)
else
""
end
end
if source_code == "" then
puts "Hello, world!"
end
source_code.chars do |c|
if (c == "Q")
puts source_code
elsif (c == "9")
puts bottles(99)
elsif (c == "+")
$accumulator += 1
else
# Nothing
end
end
if ENV["DEBUG"] == "true"
puts "=== DEBUG ==="
puts ""
puts "Total Bottles: #{$bottles}"
puts "Accumulator: #{$accumulator}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment