this took me 3 hours and 45 min to do
To run the simulation, type the following into your console (assuming you have ruby installed):
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.