- 100 clowns in a line, all wearing a red or blue balloon hat
- a clown does not know which color hat they are wearing
- all clowns face the same direction, so that first clown can see all 99 clowns and their hat colors ahead of them
- clown cannot see hats / colors behind them
- starting at first clown, they can ONLY say either "red" or "blue" (cannot pass information any other way). If the answer does not match their hat color, balloon gets popped.
What is the optimal strategy the clowns should use to ensure the most amount of clowns don't get their balloon hats popped?
To run the simulation, type the following into your console:
ruby perform_hat_ritual.rb 20
In the last number is an argument to the ruby script, which specifies the number of times to run the simulation.
If you don't have Ruby setup locally, you can do one of two things:
-
Install docker, and follow golubitsky's awesome posts on how to setup a dockerized Rails environment on Windows here and here
-
Do the below steps to install ruby on your computer.
First, make sure you have ruby installed by typing the following into your console:
ruby -v
If you don't have ruby installed, or your ruby version is less than 1.9, install the latest version of Ruby from http://rubyinstaller.org/downloads/
Now, install ruby gems (the ability to download ruby 'gems' or libraries, from the command line) by cloning ruby gems from github and running the setup file. ###Note: you'll have to make sure your powershell session is being run as administrator
cd some_directory_you_put_repos_you_clone
git clone https://github.com/rubygems/rubygems
cd rubygems
ruby setup.rb
Now that you can easily install gems, you'll want to install a useful gem called bundler, which is a sort of package manager for ruby gems:
cd ..
gem install bundler
Now clone my repo, and use bundler to install the gems required for debugging:
git clone https://github.com/discotroll65/blue_hat_red_hat.git
cd blue_hat_red_hat
bundle install
You should now be good to go.
This repo uses a gem called pry-byebug
(which is specified in the repo's Gemfile
, which bundler uses to identify which gems it should install) as a default debugger.
If you want to put a break point in the code, insert binding.pry
at your desired breakpoint, and save. As an example, the main branch has binding.pry
already in the execution script, so if everything is setup correctly, the first time you run ruby perform_hat_ritual.rb 20
, you will hit a breakpoint.
This will be the hook that allows you to go line by line, step into functions, or continue. To exit the debug session, you can type !!!
.
More info on pry-byebug
's debug commands here: https://github.com/deivid-rodriguez/pry-byebug