Most complex "Hello world" program you can justify

  • Your boss asks you to write a "hello world" program. Since you get paid for lines of code, you want to make it as complex as possible. However if you just add nonsense lines, or obviously useless or obfuscating stuff, you will never get it through code review. Therefore the challenge is:



    Write a "hello world" program which is as complex as possible under the condition that you can give a "justification" for every complexity in the code.



    The required behavior of the program is to just output a single line "Hello world" (without the quotes, but with a newline at the end) and then exit successfully.



    "Justifications" include:




    • buzzword compatibility ("Modern software is object oriented!")

    • generally accepted good programming practices ("Everyone knows that you should separate model and view")

    • maintainability ("If we do it this way, we can more easily do XXX later")

    • and of course any other justification you can imagine using (in other situations) for real code.



    Obviously silly justifications will not be accepted.



    Also, you have to "justify" your choice of language (so if you choose an inherently verbose language, you'll have to justify why it is the "right" choice). Fun languages like Unlambda or Intercal are not acceptable (unless you can give a very good justification for using them).



    The score of qualifying entries is computed as follows:




    • 1 point for each statement (or whatever the equivalent to a statement is in your language of choice).

    • 1 point for each definition of a function, type, variable etc (with the exception of the main function, where applicable).

    • 1 point for each module use statement, file include directive, namespace using statement or similar.

    • 1 point for each source file.

    • 1 point for each necessary forward declaration (if you could get rid of it by rearranging code, you have to "justify" why the arrangement you've chosen is the "right" one).

    • 1 point for each control structure (if, while, for, etc.)



    Remember that you have to "justify" each single line.



    If the chosen language is different enough that this scheme cannot be applied (and you can give a good "justification" for its use), please suggest a scoring method which most closely resembles the above for your language of choice.



    Contestants are asked to calculate the score of their entry and write it in the answer.


    any points for complex building needs? like different build files for each (self made modular) library

    Somehow, these 'gimme more, more, more' - challenges only look interesting for 5 minutes. Let's do a ProxyPoolFactoryPoolFacadePoolProxyFactory (Pool)! You need a restriction like: Finish in 20 minutes from now! Another problem is 'silly justifications will not be accepted'. It's not only subjective, it is void from the beginning, since we know, that the whole thing is silly. Okay - instead of a ProxyPoolPool, let's use something less silly, a PoolProxyProxy probably?

    @ChristopheD: While I didn't know that SO question, there's a twist in mine which is not in that one: You have to "justify" your choices (i.e. just making it more complex doesn't suffice, you'll have to give a good reason for the complexity).

    @ratchetfreak: An additional build file is an additional file. Therefore it will give an additional point as of the rules as stated. Provided you can "justify" that complexity, of course.

    @userunknown: There *is* such a restriction: You have to "justify" each choice. You'll have a hard time to justify your `ProxyPoolFactoryPoolFacadePoolProxyFactory(Pool)`.

    @userunknown: Another point: Note that overly long or complicated variable names do *not* give you points.

    Genetic Algorithm FTW!

    @elssar: Given that it's not an optimization problem, I think it will be very hard do provide a *not obviously silly* justification for it (see the requirements). Of course you are free to try.

    @celtschk isn't optimization a buzz word?

    @elssar: There's a *big* difference between *optimizing your code* and *coding an optimization*. Hmmm ... thinking about it, if the genetic algorithm is part of your *build environment* (to get optimal "Hello world" code) it might be justifiable. But no silly things like genetically "optimizing" the output string. You *have* to be able to argue that it makes the resulting executable better than the straightforward way (it doesn't need to *actually* make it better, of course, but it should at least *look* as if it could).

    I'm not sure that the *"not obviously silly"* restriction on the justification can be made to agree with the FAQ where it says *"All questions on this site [...] should have [... an] objective primary winning criterion"*.

    It seems hard to beat GNU Hello World (http://www.gnu.org/software/hello/) - the source code for version 2.7 is a 586 kB download as a compressed archive, complete with automatic tests, internationalization, documentation etc.

    Related: article includes a Hello World in audible Morse code, generated from Python: http://thelivingpearl.com/2013/01/08/morse-code-and-dictionaries-in-python-with-sound/

    I am surprised nobody did a DCOM or CORBA version so that you could run hello world from anywhere. Something like: http://www.codeproject.com/KB/COM/HelloTutorial7.aspx

    Java programmers won't have to try very hard for this one :)

  • C++, trollpost




    Most complex “Hello world” program you can justify




    #include <iostream>

    using namespace std;

    int main(int argc, char * argv[])
    {
    cout << "Hello, world!" << endl;
    return 0;
    }


    My brain cannot justify writing a longer one :)


    The best answer here.

    "using namespace std" cannot be justified! Plus your main takes arguments it doesn't use. Wasteful! ;)

    Too complex for me. What's it meant to do? Is there the possibility of adding some unit tests?

  • I will here demonstrate the power and usability of the scripting language called Python by solving a rather complex task in a graceful and efficient manner through the aforementioned scripting language's operators on – and generators of – data structures such as lists and dictionaries.



    However, I'm afraid I don't fully comprehend the use of the phrases "complex as possible" and "justification". Nevertheless, here is a rundown of my usual, quite self-explanatory and straight-forward strategy, followed by the actual implementation in Python which you will find, is quite true to the playful, high-order nature of the language:




    1. Define the alphabet – obvious first step.
      For expandability, we choose the entire ascii range.
      Note the use of the built-in list-generator that can save us for hours of
      tedious list initialization.


    2. tell how many of each letter in the alphabet we will be using.
      This is simply represented as another list!


    3. Merge these two lists into one handy dictionary,
      where the keys are ascii points and the values are the desired amount.


    4. We are now ready to start making characters!
      Start by creating a string of character out of the dictionary.
      This will contain all the characters we need in our final output, and the
      right amount of each one!


    5. Declare the desired order of the characters and initiate a new list that
      will hold our final output. With simple iteration we will put the generated
      characters into their final position and print the result!




    Here is the actual implementation



    # 1: Define alphabet:
    a = range(255)

    # 2: Letter count:
    n = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1, 1, 0, 0, 0, 0, 0, 0, 3, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0)

    # 3: Merge to dictionary:
    d = { x: y for x, y in zip(a,n) }

    # 4: 'Initialize' characters
    l = ''.join([chr(c) *n for c,n in d.items()])

    # 5: Define the order of the characters, initialize final string
    # and sort before outputting:
    z = [6,5,0,7,11,1,2,3,4,8,9]
    o = [0] * 13

    for c in l:
    i = z.pop(0)
    o[i] = c

    print ''.join(o)





    Ok, Just went for a short but stupid one and added a bunch of text rather than a TL;DR code solution


    Does the definition of `n` here count as 1 line or 11?

  • Dart's Hello World compiled to JS (2 936 012)



    http://code.google.com/p/dart/issues/detail?id=14686



    (I'll let Google justify it, though)


  • Scala, score: 62





    Okay, I throw my hat into the ring.



    ContentProvider.scala:



    /*  
    As we all know, the future is functional programming.

    And one of the mantras of pure functional programming is, to avoid mutable data
    as hell. Using case classes and case objects allows us to create very small,
    immutable Flight-Weight-Pattern like objects (Singletons, if you like).

    I'm choosing scala, because its compiled to bytecode for the JVM and therefore very
    portable. I could of course have implemented it in Java, but as we all know,
    Javacode is boilerplaty, while scala is a concise language.

    S: for easy grepping of scoring hints.
    Scoring summary:

    1 import
    3 control structures
    8 function calls
    22 function definitions
    14 type definitions
    14 files: Seperate files speed up the compilation process,
    if you only happen to make a local change.
    */

    /**
    To change the content and replace it with something else later, we generate
    a generic Content trait, which will be 'Char' in the beginning, but could be Int or
    something.

    S: 1 type definition.
    S: 1 function
    */
    trait ContentProvider [T] {
    // ce is the content-element, but we like to stay short and lean.
    def ce () : T
    }


    HWCChain.scala:



    //S:  1 import, for the tailcall annotation later. 
    import annotation._

    /**
    HWCChain is a Chain of HelloWordCharacters, but as a lean, concise language,
    we do some abbrev. here.
    We need hasNext () and next (), which is the iterator Pattern.

    S: 1 type
    S: 2 functions definitions
    S: 4 function calls
    S: 1 if
    */
    trait HWCChain[T] extends Iterator [HWCChain[T]] with ContentProvider[T] {
    // tailrec is just an instruction for the compiler, to warn us, if this code
    // can't be tail call optimized.
    @tailrec
    final def go () : Unit = {
    // ce is our ContentProvider.ce
    System.out.print (ce);
    // and here is our iterator at work, hasNext and next:
    if (hasNext ()) next ().go ()
    }
    // per default, we have a next element (except our TermHWWChain, see close to bottom)
    // this follows the DRY-principle, and reduces the code drastically.
    override def hasNext (): Boolean = true
    }


    HHWCChain.scala:



    /**
    This is a 'H'-element, followed by the 'e'-Element.
    S: 1 type
    S: 2 functions
    */
    case object HHWCChain extends HWCChain[Char] with ContentProvider[Char] {
    override def ce = 'H'
    override def next = eHWCChain
    }


    eHWCChain.scala:



    /*
    and here is the 'e'-Element, followed by l-Element 1, which is a new Type

    S: 1 type
    S: 2 functions
    */
    case object eHWCChain extends HWCChain[Char] {
    override def ce = 'e'
    override def next = new indexedLHWCChain (1)
    }


    theLThing.scala:



    /**
    we have to distinguish the first, second and third 'l'-thing.
    But of course, since all of them provide a l-character,
    we extract the l for convenient reuse. That saves a lotta code, boy!

    S: 1 type
    S: 1 function
    */
    trait theLThing extends HWCChain[Char] {
    override def ce = 'l'
    }


    indexedLHWCChain.scala:



    /**
    depending on the l-number, we either have another l as next, or an o, or the d.
    S: 1 type
    S: 1 function definition
    S: 2 function calls
    S: 1 control structure (match/case)
    */
    case class indexedLHWCChain (i: Int) extends theLThing {
    override def next = i match {
    case 1 => new indexedLHWCChain (2)
    case 2 => new indexedOHWCChain (1)
    case _ => dHWCChain
    }
    }


    theOThing.scala:



    // see theLTHing ...
    //S: 1 type
    //S: 1 function
    trait theOThing extends HWCChain[Char] {
    override def ce = 'o'
    }


    indexedOHWCChain.scala:



    // and indexedOHWCCHain ...
    //S: 1 type
    //S: 1 function definition
    //S: 1 function call
    //S: 1 control structure
    case class indexedOHWCChain (i: Int) extends theOThing {
    override def next = i match {
    case 1 => BlankHWCChain
    case _ => rHWCChain
    }
    }


    BlankHWCChain.scala:



    // and indexedOHWCCHain ...
    //S: 1 type
    //S: 2 function definitions
    case object BlankHWCChain extends HWCChain[Char] {
    override def ce = ' '
    override def next = WHWCChain
    }


    WHWCChain.scala:



    //S: 1 type
    //S: 2 function definitions
    case object WHWCChain extends HWCChain[Char] {
    override def ce = 'W'
    override def next = new indexedOHWCChain (2)
    }


    rHWCChain.scala:



    //S: 1 type 
    //S: 2 function definitions
    case object rHWCChain extends HWCChain[Char] {
    override def ce = 'r'
    override def next = new indexedLHWCChain (3)
    }


    dHWCChain.scala:



    //S: 1 type
    //S: 2 function definitions
    case object dHWCChain extends HWCChain[Char] {
    override def ce = 'd'
    override def next = TermHWCChain
    }


    TermHWCChain.scala:



    /*
    Here is the only case, where hasNext returns false.
    For scientists: If you're interested in terminating programs, this type is
    for you!

    S: 1 type
    S: 3 function definitions
    */
    case object TermHWCChain extends HWCChain[Char] {
    override def ce = '\n'
    override def hasNext (): Boolean = false
    override def next = TermHWCChain // dummy - has next is always false
    }


    HelloWorldCharChainChecker.scala:



    /* 
    S: 1 type
    S: 1 function call
    */
    object HelloWorldCharChainChecker extends App {
    HHWCChain.go ()
    }


    Of course, for a pure functional approach, 0 stinky variables. Everything is layed out in the type system and straight forward. A clever compiler can optimize it down to the bare nothing.



    The program is clear, simple and easy to understand. It is easy testable and generic and avoids the trap of overengineering (my team wanted to recode the indexedOHWCChain and indexedLHWCChain to a common secondary trait, which has an array of targets and a length field, but that would have been just silly!).


    Where are the 14 files?

    After every closing brace in column 0, a new file is made. One file per object, class and trait. That speeds up the build process, if you only modify a single object.

    Then please make that clear in the answer itself.

    Okay, I did so.

    This is not `complex` as the original question requests. It is just very `verbose`. There is a difference.

    +1 for "Javacode is boilerplaty, while scala is a concise language." followed by the most boilerplaty code I've seen.

  • Pure Bash no fork (some count, seem to be approx 85...)




    • 6 function initRotString 2 variables, 3 statments

    • 14 function binToChar 2 variables, 7 statements + sub define: 1 func, 1 var, 2 stat

    • 34 function rotIO 9 variables, 25 statments

    • 9 function rle 4 variables, 5 statments

    • 22 MAIN 13 variables, 9 statments



    Features:




    • Two level RLE: first binary encode each chars and second for repeated chars

    • Key based modified rot13: The rotIO function perform rotation like Rot13, but on 96 values instead of 26 (*rot47), but shifted by submited key.

    • Second version use gzip and uuencode via perl (more commonly installed than uudecode)



    Complete rewrite (bugs corrections, ascii-art drawing and two level rle):



    #!/bin/bash

    BUNCHS="114 11122 112111 11311 1213 15 21112 11311 1123 2121 12112 21211"
    MKey="V922/G/,2:"

    export RotString=""
    function initRotString() {
    local _i _char
    RotString=""
    for _i in {1..94} ;do
    printf -v _char "\\%03o" $((_i+32))
    printf -v RotString "%s%b" "$RotString" $_char
    done
    }


     



    function rotIO() {
    local _line _i _idx _key _cidx _ckey _o _cchar _kcnt=0
    while read -r _line ;do
    _o=""
    for (( _i=0 ; _i < ${#_line} ; _i++)) ;do
    ((_kcnt++ ))
    _cchar="${_line:_i:1}"
    [ "${_cchar//\(}" ] || _cchar="\("
    [ "${_cchar//\*}" ] || _cchar="\*"
    [ "${_cchar//\?}" ] || _cchar="\?"
    [ "${_cchar//\[}" ] || _cchar="\["
    [ "${_cchar//\\}" ] || _cchar='\\'
    if [ "${RotString//${_cchar}*}" == "$RotString" ] ;then
    _o+="${_line:_i:1}"
    else
    _kchar="${1:_kcnt%${#1}:1}"
    [ "${_kchar//\(}" ] || _kchar="\("
    [ "${_kchar//\*}" ] || _kchar="\*"
    [ "${_kchar//\?}" ] || _kchar="\?"
    [ "${_kchar//\[}" ] || _kchar="\["
    [ "${_kchar//\\}" ] || _kchar='\\'
    _key="${RotString//${_kchar}*}"
    _ckey=${#_key}
    _idx="${RotString//${_cchar}*}"
    _cidx=$(((1+_ckey+${#_idx})%94))
    _o+=${RotString:_cidx:1}
    fi; done
    if [ "$_o" ] ; then
    echo "$_o"
    fi ; done ; }


     



    function rle() {
    local _out="" _c=1 _l _a=$1
    while [ "${_a}" ] ; do
    printf -v _l "%${_a:0:1}s" ""
    _out+="${_l// /$_c}"
    _a=${_a:1} _c=$((1-_c))
    done
    printf ${2+-v} $2 "%s" $_out
    }
    function binToChar() {
    local _i _func="local _c;printf -v _c \"\\%o\" \$(("
    for _i in {0..7} ;do
    _func+="(\${1:$_i:1}<<$((7-_i)))+"
    done
    _func="${_func%+}));printf \${2+-v} \$2 \"%b\" \$_c;"

    eval "function ${FUNCNAME}() { $_func }"
    $FUNCNAME [email protected]
    }

    initRotString


     



    for bunch in "${BUNCHS[@]}" ; do
    out=""
    bunchArray=($bunch)
    for ((k=0;k<${#bunchArray[@]};k++)) ; do
    enum=1
    if [ "${bunchArray[$k]:0:1}" == "-" ];then
    enum=${bunchArray[$k]:1}
    ((k++))
    fi
    ltr=${bunchArray[$k]}
    rle $ltr binltr
    printf -v bin8ltr "%08d" $binltr
    binToChar $bin8ltr outltr
    printf -v mult "%${enum}s" ""
    out+="${mult// /$outltr}"
    done
    rotIO "$MKey" <<< "$out"
    done


    (The key used V922/G/,2: is based on HelloWorld too,
    but that's no matter;)



    Result (as requested):



    Hello world!


    There is another version:



    #!/bin/bash

    eval "BUNCHS=(" $(perl <<EOF | gunzip
    my\$u="";sub d{my\$l=pack("c",32+.75*length(\$_[0]));print unpack("u",\$l.\$
    _[0]);"";}while(<DATA>){tr#A-Za-z0-9+/##cd;tr#A-Za-z0-9+/# -_#;\$u.=\$_;while
    (\$u=~s/(.{80})/d(\$1)/egx){};};d(\$u);__DATA__
    H4sIAETywVICA8VZyZLcMAi9z1e4+q6qAHIr+f8fi7UgyQYs3DOp5JBxywKxPDZr27bthRFgA4B9C0Db
    8YdoC+UB6Fjewrs8A8TyFzGv4e+2iLh9HVy2sI+3lQdk4pk55hdIdQNS/Qll2/FUuAD035V3Y1gEAUI4
    0yBg3xxnaZqURYvAXLoi2Hj1N4U84HQsy1MPLiRC4qpj3CgKxc6qVwMB8+/0sR0/k8a+LZ4M2o6tUu1V
    /oMM5SZWBLslsdqtsMaTvbG9gqpbU/d4iDgrmtXXtD3+0bBVleJ4o+hpYAGH1dkBhRfN7mjeapbpPu8P
    1QzsKRLmCsNvk2Hq6ntYJjOirGaks58ZK2x7nDHKj7H8Fe5sK21btwKDvZtCxcKZuPxgL0xY5/fEWmVx
    OxEfHAdptnqcIVI4F15v2CYKRkXsMVBDsOzPNqsuOBjXh8mBjA+Om/mkwruFSTwZDlC30is/vYiaRkWG
    otG0QDVsz2uHQwP+6usNpwYHDgbJgvPiWOfsQAbBW6wjFHSdzoPmwtNyckiF1980cwrYXyyFqCbS1dN3
    L60+yE727rSTeFDgc+fWor5kltEnJLsKkqSRWICZ2WWTEAmve5JmK/yHnNxYj26oX+0nTyXViwaMlwh2
    CJW1ugBEargbGtJFhigVFCs6Tn36GFjThTIUukPIQqSyMcgso6stk8xnxp8K9Cr2HDhhFR3glpa6ZiKO
    HfIkFSt+PoO7wB7hjaEc7tJEk8w8CNcB5uB1ySaWJVsZRHzqLoPTMvaSp1wocFezmxI/M5SfptDkyO3f
    gJNeUUNaNweooE6xkaNe3TUxAW+taR+jGoo0cCtHJC3e+xGXLKq1CKumAbW0kDxtldGLLfLLDeWicIkg
    1jOEFtadl9D9scGSm9ESfTR/WngEIu3Eaqv0lEzbsm7aPfJVvTyBmBY/jZZIslEDaNnH+Ojs4KwTYZ/+
    Lx8D1ulL7YmUOPkhNur0piXlMH2QkcWFvMs36crIqVrSv3p7TKjhzwMba3axh6WP2SwwQKvBc2ind7E/
    wUhLlLujdK3KL67HVG2Wf8pj7T1zBjBOGT22VUPcY9NdNRXOWNUcw4dqSvJ3V8+lMptHtQ+696DdiPo9
    z/ks2lI9C5aBkJ9gpNaG/fkk0UYmTyHViWWDYTShrq9OeoZJvi7zBm3rLhRpOR0BqpUmo2T/BKLTZ/HV
    vLfsa40wdlDezKUBP5PNF8RP1nx2WuPkCGeV1YNQ0aDuJL5c5OBN72m1Oo7PVpWZ7+uIb6BMzwuWVnb0
    2cYxyciKaRneNRi5eQWfwYKvCLr5uScSh67/k1HS0MrotsPwHCbl+up00Y712mtvd33j4g/4UnNvyahe
    hLabuPm+71jmG+l6v5qv2na+OtwHL2jfROv/+daOYLr9LZdur6+/stxCnQsgAAA=
    EOF
    ) ")"

    MKey="V922/G/,2:"
    export RotString=""

    function initRotString() {
    local _i _char
    RotString=""
    for _i in {1..94} ;do
    printf -v _char "\\%03o" $((_i+32))
    printf -v RotString "%s%b" "$RotString" $_char
    done
    }
    function rotIO() {
    local _line _i _idx _key _cidx _ckey _o _cchar _kcnt=0
    while read -r _line ;do
    _o=""
    for (( _i=0 ; _i < ${#_line} ; _i++)) ;do
    ((_kcnt++ ))
    _cchar="${_line:_i:1}"
    [ "${_cchar//\(}" ] || _cchar="\("
    [ "${_cchar//\*}" ] || _cchar="\*"
    [ "${_cchar//\?}" ] || _cchar="\?"
    [ "${_cchar//\[}" ] || _cchar="\["
    [ "${_cchar//\\}" ] || _cchar='\\'
    if [ "${RotString//${_cchar}*}" == "$RotString" ] ;then
    _o+="${_line:_i:1}"
    else
    _kchar="${1:_kcnt%${#1}:1}"
    [ "${_kchar//\(}" ] || _kchar="\("
    [ "${_kchar//\*}" ] || _kchar="\*"
    [ "${_kchar//\?}" ] || _kchar="\?"
    [ "${_kchar//\[}" ] || _kchar="\["
    [ "${_kchar//\\}" ] || _kchar='\\'
    _key="${RotString//${_kchar}*}"
    _ckey=${#_key}
    _idx="${RotString//${_cchar}*}"
    _cidx=$(((1+_ckey+${#_idx})%94))
    _o+=${RotString:_cidx:1}
    fi; done
    if [ "$_o" ] ; then
    echo "$_o"
    fi; done
    }
    function rle() {
    local _out="" _c=1 _l _a=$1
    while [ "${_a}" ] ; do
    printf -v _l "%${_a:0:1}s" ""
    _out+="${_l// /$_c}"
    _a=${_a:1} _c=$((1-_c))
    done
    printf ${2+-v} $2 "%s" $_out
    }
    function binToChar() {
    local _i _func="local _c;printf -v _c \"\\%o\" \$(("
    for _i in {0..7} ;do
    _func+="(\${1:$_i:1}<<$((7-_i)))+"
    done
    _func="${_func%+}));printf \${2+-v} \$2 \"%b\" \$_c;"

    eval "function ${FUNCNAME}() { $_func }"
    $FUNCNAME [email protected]
    }

    initRotString

    for bunch in "${BUNCHS[@]}" ; do
    out=""
    bunchArray=($bunch)
    for ((k=0;k<${#bunchArray[@]};k++)) ; do
    enum=1
    if [ "${bunchArray[$k]:0:1}" == "-" ];then
    enum=${bunchArray[$k]:1}
    ((k++))
    fi
    ltr=${bunchArray[$k]}
    rle $ltr binltr
    printf -v bin8ltr "%08d" $binltr
    binToChar $bin8ltr outltr
    printf -v mult "%${enum}s" ""
    out+="${mult// /$outltr}"
    done
    rotIO "$MKey" <<< "$out"
    done


    Using same key and may render something like:



                  _   _      _ _                            _     _ _
      | | | | ___| | | ___ __ _____ _ __| | __| | |
      | |_| |/ _ \ | |/ _ \ \ \ /\ / / _ \| '__| |/ _` | |
      | _ | __/ | | (_) | \ V V / (_) | | | | (_| |_|
      |_| |_|\___|_|_|\___/ \_/\_/ \___/|_| |_|\__,_(_)
     
    ▐▌ █ ▐▙ █ █ █ ▗▛▀▙ ▟▜▖ ▗█ ▗█▌ ▗█▖
    ▐▙▄█ ▀▜▖▝▙▀▙▝▙▀▙▐▌ █ ▐▛▙█▗▛▀▙▐▌▖█ ▜▄▛▗▛▀▙ ▀▜▖▝▙▛▙ ▄▛▐▌▖█ █ ▗▛▐▌ ▝█▘
    ▐▌ █▗▛▜▌ █▄▛ █▄▛▝▙▄█ ▐▌▝█▐▛▀▀▐▙▙█ █ ▐▛▀▀▗▛▜▌ █ ▟▘▄▝▙▗▛ █ ▝▀▜▛ ▀
    ▝▘ ▀ ▀▘▀▗█▖ ▗█▖ ▗▄▄▛ ▝▘ ▀ ▀▀▘ ▀▝▘ ▝▀▘ ▀▀▘ ▀▘▀▝▀▘ ▝▀▀▀ ▝▀ ▀▀▀ ▀▀ ▀
     
      ▟▙█▖▟▙█▖ ▜▌ █ █ ▝▘ █ ▝█ ▟▙█▖▟▙█▖
      ███▌███▌ ▟▀▟▘▟▀▜▖▟▀▜▖▐▌▟▘ ▝▀▙ ▀█▀ ▀█▀ ▜▌ ▀█▀ █ █ ▟▀█ ▟▀▜▖ ███▌███▌
      ▝█▛ ▝█▛ ▜▄█ █▀▀▘█▀▀▘▐▛▙ ▟▀█ █▗▖ █▗▖ ▐▌ █▗▖█ █ █ █ █▀▀▘ ▝█▛ ▝█▛
      ▝ ▝ ▄▄▛ ▝▀▀ ▝▀▀ ▀▘▝▘ ▝▀▝▘ ▝▀ ▝▀ ▀▀ ▝▀ ▝▀▝▘▝▀▝▘▝▀▀ ▝ ▝
     
    ... And thank you for reading!!


    Hello world and happy new year 2014


    ... But it's not *as complex as possible*! I can do a lot worst :->>

  • Everyone knows that Moore's Law has taken a new turn, and that all real advances in computing power in the next decade will come in the GPU. With that in mind, I've used LWJGL to write a blazingly fast Hello World program that fully takes advantage of the GPU to generate the string "Hello World."



    Since I'm writing java, it is idiomatic to start by copying and pasting someone elses code, I used http://lwjgl.org/wiki/index.php?title=Sum_Example



    package magic;
    import org.lwjgl.opencl.Util;
    import org.lwjgl.opencl.CLMem;
    import org.lwjgl.opencl.CLCommandQueue;
    import org.lwjgl.BufferUtils;
    import org.lwjgl.PointerBuffer;
    import org.lwjgl.opencl.CLProgram;
    import org.lwjgl.opencl.CLKernel;

    import java.nio.IntBuffer;
    import java.util.List;

    import org.lwjgl.opencl.CL;
    import org.lwjgl.opencl.CLContext;
    import org.lwjgl.opencl.CLDevice;
    import org.lwjgl.opencl.CLPlatform;

    import static org.lwjgl.opencl.CL10.*;

    public class OpenCLHello {
    static String letters = "HeloWrd ";

    // The OpenCL kernel
    static final String source =
    ""
    + "kernel void decode(global const int *a, global int *answer) { "
    + " unsigned int xid = get_global_id(0);"
    + " answer[xid] = a[xid] -1;"
    + "}";

    // Data buffers to store the input and result data in
    static final IntBuffer a = toIntBuffer(new int[]{1, 2, 3, 3, 4, 8, 5, 4, 6, 3, 7});
    static final IntBuffer answer = BufferUtils.createIntBuffer(11);

    public static void main(String[] args) throws Exception {
    // Initialize OpenCL and create a context and command queue
    CL.create();
    CLPlatform platform = CLPlatform.getPlatforms().get(0);
    List<CLDevice> devices = platform.getDevices(CL_DEVICE_TYPE_GPU);
    CLContext context = CLContext.create(platform, devices, null, null, null);
    CLCommandQueue queue = clCreateCommandQueue(context, devices.get(0), CL_QUEUE_PROFILING_ENABLE, null);

    // Allocate memory for our input buffer and our result buffer
    CLMem aMem = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, a, null);
    clEnqueueWriteBuffer(queue, aMem, 1, 0, a, null, null);

    CLMem answerMem = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR, answer, null);
    clFinish(queue);

    // Create our program and kernel
    CLProgram program = clCreateProgramWithSource(context, source, null);
    Util.checkCLError(clBuildProgram(program, devices.get(0), "", null));
    // sum has to match a kernel method name in the OpenCL source
    CLKernel kernel = clCreateKernel(program, "decode", null);

    // Execution our kernel
    PointerBuffer kernel1DGlobalWorkSize = BufferUtils.createPointerBuffer(1);
    kernel1DGlobalWorkSize.put(0, 11);
    kernel.setArg(0, aMem);
    kernel.setArg(1, answerMem);
    clEnqueueNDRangeKernel(queue, kernel, 1, null, kernel1DGlobalWorkSize, null, null, null);

    // Read the results memory back into our result buffer
    clEnqueueReadBuffer(queue, answerMem, 1, 0, answer, null, null);
    clFinish(queue);
    // Print the result memory

    print(answer);

    // Clean up OpenCL resources
    clReleaseKernel(kernel);
    clReleaseProgram(program);
    clReleaseMemObject(aMem);
    clReleaseMemObject(answerMem);
    clReleaseCommandQueue(queue);
    clReleaseContext(context);
    CL.destroy();
    }





    /** Utility method to convert int array to int buffer
    * @param ints - the float array to convert
    * @return a int buffer containing the input float array
    */
    static IntBuffer toIntBuffer(int[] ints) {
    IntBuffer buf = BufferUtils.createIntBuffer(ints.length).put(ints);
    buf.rewind();
    return buf;
    }


    /** Utility method to print an int buffer as a string in our optimized encoding
    * @param answer2 - the int buffer to print to System.out
    */
    static void print(IntBuffer answer2) {
    for (int i = 0; i < answer2.capacity(); i++) {
    System.out.print(letters.charAt(answer2.get(i) ));
    }
    System.out.println("");
    }

    }

  • Assembly (x86, Linux/Elf32): 55 points



    Everyone knows that when you want a fast program, you need to write in assembly.



    Sometimes we can't rely on ld doing its job properly - For optimal performance, it's preferable to build our own Elf header for our hello world executable. This code requires only nasm to build, so is very portable. It relies on no external libraries or runtimes.



    Every line and statement is absolutely crucial to the correct functioning of the program - there is no cruft, nothing can be omitted.



    Moreover, this really is the shortest way to do it without using the linker - there are no unnecessary loops or declarations to bloat out the answer.



    BITS 32

    org 0x08048000

    ehdr: ; Elf32_Ehdr
    db 0x7F, "ELF", 1, 1, 1, 0 ; e_ident
    times 8 db 0
    dw 2 ; e_type
    dw 3 ; e_machine
    dd 1 ; e_version
    dd _start ; e_entry
    dd phdr - $$ ; e_phoff
    dd 0 ; e_shoff
    dd 0 ; e_flags
    dw ehdrsize ; e_ehsize
    dw phdrsize ; e_phentsize
    dw 1 ; e_phnum
    dw 0 ; e_shentsize
    dw 0 ; e_shnum
    dw 0 ; e_shstrndx

    ehdrsize equ $ - ehdr

    phdr: ; Elf32_Phdr
    dd 1 ; p_type
    dd 0 ; p_offset
    dd $$ ; p_vaddr
    dd $$ ; p_paddr
    dd filesize ; p_filesz
    dd filesize ; p_memsz
    dd 5 ; p_flags
    dd 0x1000 ; p_align

    phdrsize equ $ - phdr

    section .data
    msg db 'hello world', 0AH
    len equ $-msg

    section .text
    global _start
    _start: mov edx, len
    mov ecx, msg
    mov ebx, 1
    mov eax, 4
    int 80h

    mov ebx, 0
    mov eax, 1
    int 80h

    filesize equ $ - $$


    Scoring




    • "Statements" (counting mov, int): 8

    • "Functions, types, variables" (counting org, db, dw, dd, equ, global _start): 37

    • "Source files": 1

    • "Forward declarations" (counting dd _start, dd filesize, dw ehdrsize, dw phdrsize: 4

    • "Control structures" (counting ehdr:, phdr:, section .data, ,section .text, _start:): 5


  • PHP/HTML/CSS (88pts)



    All the code is available here : https://github.com/martin-damien/code-golf_hello-world




    • This "Hello World" use the Twig template language for PHP (http://twig.sensiolabs.org/).

    • I use an autoload mecanism to simply load classes on the fly.

    • I use a Page class that could handle as many type of elements (implementing the XMLElement interface) and restitute all those lement in a correct XML format.

    • Finally, I use shiny CSS to display a beautifull "Hello World" :)



    index.php



    <?php

    /*
    * SCORE ( 18 pts )
    *
    * file : 1
    * statements : 11
    * variables : 6 (arrays and class instance are counted as a variable)
    */

    /*
    * We use the PHP's autoload function to load dynamicaly classes.
    */
    require_once("autoload.php");

    /*
    * We use a template engine because as you know it is far better
    * to use MVC :-)
    */
    require_once("lib/twig/lib/Twig/Autoloader.php");
    Twig_Autoloader::register();

    /*
    * We create a new Twig Environment with debug and templates cache.
    */
    $twig = new Twig_Environment(

    new Twig_Loader_Filesystem(

    "design/templates" /* The place where to look for templates */

    ),
    array(
    'debug' => true,
    'cache' => 'var/cache/templates'
    )

    );
    /*
    * We add the debug extension because we should be able to detect what is wrong if needed
    */
    $twig->addExtension(new Twig_Extension_Debug());

    /*
    * We create a new page to be displayed in the body.
    */
    $page = new Page();

    /*
    * We add our famous title : Hello World !!!
    */
    $page->add( 'Title', array( 'level' => 1, 'text' => 'Hello World' ) );

    /*
    * We are now ready to render the content and display it.
    */
    $final_result = $twig->render(

    'pages/hello_world.twig',
    array(

    'Page' => $page->toXML()

    )

    );

    /*
    * Everything is OK, we can print the final_result to the page.
    */
    echo $final_result;

    ?>


    autoload.php



    <?php

    /*
    * SCORE ( 7 pts )
    *
    * file : 1
    * statements : 4
    * variables : 1
    * controls: 1
    */

    /**
    * Load automaticaly classes when needed.
    * @param string $class_name The name of the class we try to load.
    */
    function __hello_world_autoload( $class_name )
    {

    /*
    * We test if the corresponding file exists.
    */
    if ( file_exists( "classes/$class_name.php" ) )
    {
    /*
    * If we found it we load it.
    */
    require_once "classes/$class_name.php";
    }

    }

    spl_autoload_register( '__hello_world_autoload' );

    ?>


    classes/Page.php



    <?php

    /*
    * SCORE ( 20 pts )
    *
    * file : 1
    * statements : 11
    * variables : 7
    * controls : 1
    */

    /**
    * A web page.
    */
    class Page
    {

    /**
    * All the elements of the page (ordered)
    * @var array
    */
    private $elements;

    /**
    * Create a new page.
    */
    public function __construct()
    {
    /* Init an array for elements. */
    $this->elements = array();
    }

    /**
    * Add a new element to the list.
    * @param string $class The name of the class we wants to use.
    * @param array $options An indexed array of all the options usefull for the element.
    */
    public function add( $class, $options )
    {
    /* Add a new element to the list. */
    $this->elements[] = new $class( $options );
    }

    /**
    * Render the page to XML (by calling the toXML() of all the elements).
    */
    public function toXML()
    {

    /* Init a result string */
    $result = "";

    /*
    * Render all elements and add them to the final result.
    */
    foreach ( $this->elements as $element )
    {
    $result = $result . $element->toXML();
    }

    return $result;

    }

    }

    ?>


    classes/Title.php



    <?php

    /*
    * SCORE ( 13 pts )
    *
    * file : 1
    * statements : 8
    * variables : 4
    *
    */

    class Title implements XMLElement
    {

    private $options;

    public function __construct( $options )
    {
    $this->options = $options;
    }

    public function toXML()
    {

    $level = $this->options['level'];
    $text = $this->options['text'];

    return "<h$level>$text</h$level>";

    }

    }

    ?>


    classes/XMLElement.php



    <?php

    /*
    * SCORE ( 3 pts )
    *
    * file : 1
    * statements : 2
    * variables : 0
    */

    /**
    * Every element that could be used in a Page must implements this interface !!!
    */
    interface XMLElement
    {

    /**
    * This method will be used to get the XML version of the XMLElement.
    */
    function toXML();

    }

    ?>


    design/stylesheets/hello_world.css



    /*
    * SCORE ( 10 pts )
    *
    * file : 1
    * statements : 9
    */

    body
    {
    background: #000;
    }

    h1
    {
    text-align: center;
    margin: 200px auto;
    font-family: "Museo";
    font-size: 200px; text-transform: uppercase;
    color: #fff;
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #ff00de, 0 0 70px #ff00de, 0 0 80px #ff00de, 0 0 100px #ff00de, 0 0 150px #ff00de;
    }


    design/templates/layouts/pagelayout.twig



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">

    <!--

    SCORE ( 11 pts )

    file: 1
    statements: html, head, title, css, body, content, block * 2 : 8
    variables : 2 blocks defined : 2

    -->

    <head>

    <title>{% block page_title %}{% endblock %}</title>
    <link href="design/stylesheets/hello_world.css" rel="stylesheet" type="text/css" media="screen" />

    </head>

    <body>
    <div id="content">
    {% block page_content %}{% endblock %}
    </div>
    </body>

    </html>


    design/templates/pages/hello_world.twig



    {#

    SCORE ( 6 pts )

    file : 1
    statements : 4
    variables : 1

    #}

    {% extends 'layouts/pagelayout.twig' %}

    {% block page_title %}Hello World{% endblock %}

    {% block page_content %}
    {# Display the content of the page (we use raw to avoid html_entities) #}
    {{ Page|raw }}
    {% endblock %}

  • Brainfuck


    369 expression, 29 while loops = 398


    > ; first cell empty
    ;; All the chars made in a generic way
    ;; by first adding the modulus of char by
    ;; 16 and then adding mutiples of 16
    ;; This simplifies adding more characters
    ;; for later versions
    ------>>++++[-<++++>]<[-<+>] ; CR
    +>>++++[-<++++>]<[-<++>] ; !
    ++++>>++++[-<++++>]<[-<++++++>] ; d
    ---->>++++[-<++++>]<[-<+++++++>] ; l
    ++>>++++[-<++++>]<[-<+++++++>] ; r
    ->>++++[-<++++>]<[-<+++++++>] ; o
    +++++++>>++++[-<++++>]<[-<+++++++>] ; w
    >>++++[-<++++>]<[-<++>] ; space
    ---->>++++[-<++++>]<[-<+++>] ; comma
    ->>++++[-<++++>]<[-<+++++++>] ; o
    ---->>++++[-<++++>]<[-<+++++++>] ; l
    ---->>++++[-<++++>]<[-<+++++++>] ; l
    +++++>>++++[-<++++>]<[-<++++++>] ; e
    -------->>++++[-<++++>]<[-<+++++++>]; h
    <[.<] ; print until the first empty cell

    Output as from K&R The C Programming Language example:


    hello, world!

  • So, I have a very... ... ...peculiar manager. He has this strange idea that the simpler a program is, the more beautiful, the more artistic it should be. Since the Hello World is arguably one of the easiest programs to write, he has asked for something so great he could hang on the wall. After doing some research, he insisted that the thing be written in Piet.



    Now, I am not one to question the merits of the most intelligent person to ever grace upper management, so I was tasked with "writing" this program, which can be run on this online piet interpreter. Maybe it's time to look for a more sane manager...



    enter image description here


    Plus one for being esoteric. Also, there are dozens of unique "Hello World" programs in Piet, my favorite is the animated one.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM