fredag 23 september 2016

A slice of pi

I baked a pi with ruby:

()()()()()()
|\3.1415926|
|:\53589793|
\::\2384626|
 \::\433832|
  \::\79502|
   \::\8841|
    \::\971|
     \::\69|
      \::\3|
       \__\|

I did a codegolf the other day where the objective was to print out the above ASCII-art in the shortest amount of code possible. The code I came up with looks like this:
[0,2384626,433832,79502,8841,971,69,3,1].map{|n|puts n<1?"()"*6+"\n|\\3.1415926|\n|:\\53589793|":"\\#{n>1?"::\\#{n}":"__\\"}|".rjust(12)}
view raw pi.rb hosted with ❤ by GitHub
It is quite hard to read so here is a "ungolfed" version:
for n in [0,2384626,433832,79502,8841,971,69,3,1]
if n < 1 # top part of the pie
puts "()"*6+"\n|\\3.1415926|\n|:\\53589793|"
else
if n > 1 # filling of the pie
puts "\\::\\#{n}|".rjust(12)
else # bottom part of the pie
puts "\\__\\|".rjust(12)
end
end
end
view raw pi-explained.rb hosted with ❤ by GitHub
What I did was to create an array with the sub-sections of the pie and then traverse through that array and print out \\::\\n| where n is the current section of pi. For the bottom part I just printed out \\__\\|. I used rjust to get the pie to be right alligned.

Anyway, that's it for today. I've gotta go bake some raspberry pi, thanks for reading my post!

Inga kommentarer:

Skicka en kommentar