Created
July 6, 2015 10:10
-
-
Save monolar/adce7455b464fd60e293 to your computer and use it in GitHub Desktop.
just playing around to solve this riddle: http://www.spiegel.de/wissenschaft/mensch/raetsel-der-woche-wie-viele-schliessfaecher-stehen-offen-a-1041982.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# http://www.spiegel.de/wissenschaft/mensch/raetsel-der-woche-wie-viele-schliessfaecher-stehen-offen-a-1041982.html | |
require 'tco' | |
NUMBER_OF_DOORS = ARGV[0].to_i || 10 | |
INITAL_STATE = false # false is closed, true is opened | |
DOORS = (1..NUMBER_OF_DOORS).map { false } | |
def run(n) | |
DOORS.each_with_index do |door,index| | |
if ((index+1) % n) == 0 | |
DOORS[index] = !DOORS[index] | |
end | |
end | |
end | |
def render | |
DOORS.each do |d| | |
if d | |
print " ".bg("#00DD00") | |
else | |
print " ".bg("#DD0000") | |
end | |
end | |
print "\n" | |
end | |
(1..NUMBER_OF_DOORS).each do |n| | |
run(n) | |
render() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment