Skip to content

Instantly share code, notes, and snippets.

@dharmatech
Last active June 3, 2016 22:25
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 dharmatech/b7f01ddf5ea425f1d290c32e8809abce to your computer and use it in GitHub Desktop.
Save dharmatech/b7f01ddf5ea425f1d290c32e8809abce to your computer and use it in GitHub Desktop.

The 100 doors example is a nice one-liner.

What about a way to visualize the door states over time? Here's a short program to do this:

use v6;

sub display-doors (\doors)
{
    for doors { print $_ == True ?? '*' !! ' '; }

    print "\n";
}

sub MAIN(\door-count, \passes, Bool :$random)
{
    my \doors = [];

    doors[^door-count] = $random ??
        (loop { (True, False).pick }) !!
        (loop { False              });

    display-doors doors;

    for 1..passes -> \n
    {
        for doors[n-1, (n-1)+n ... *] { $_ = !$_ };

        display-doors doors;
    }

}

The program is called with the number of doors and how many passes over the doors to make:

The output sorta looks like elementary cellular automaton.

Let's run it again in xterm with "Tiny" font:

One more time with "Unreadable" font:

By default, the doors all start out closed. If you pass the --random flag, they'll start out in a random state:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment