Thursday, February 12, 2009

Brain teasers

For some reason, I often think of little puzzles in the shower - probably because showering itself isn't terribly exciting. Here's two of them (they're not particularly difficult but they were good for the length of a shower):
1) I have a digital clock that expresses time in 24-hours. Just for fun, let's say you have no colon or spaces in between the digits, so midnight is 000, 9:15am is 915, 12 noon is 1200, 3:35pm is 1535, etc. How many times during a 24-hour day would the number look exactly the same in a mirror as it does on the face? So, for example, 12 midnight (000) would be the first. The answer turned out to be more than I thought as an initial guesstimate.
2) For whatever reason, I have two dice and I want to pick a random month of the year. What's the best way to do it?

3 comments:

Anonymous said...

1) looks like it is 16 times:
#nate.rb
winners = []
(0..59).each do |minute|

(0..23).each do |hour|
time = ("%02d" % hour) + "%02d" % minute
puts time if time == time.reverse
end
end

Nate said...

Well, there's a couple of things wrong with that:
1) The hour is expressed as two digits, which it isn't on my clock..
2) This doesn't take the mirroring effect into account, such that for example the number '3' looks like the letter 'E', and the number '2' looks like the number '5'. What you really need to do is come up with a "inverting" function, run it, and then compare the result with the original #. A quick and simple Perl hack would be something like:

perl -e 'foreach $h(0..12) { foreach $m(0..60) { $m="0$m" if ($m<10); @now=split (//, "$h$m"); $mirror=reverse @now; next if $mirror=~/3|4|6|7|9/; $mirror=~tr/25/52/; print "$mirror\n" if ("$mirror" eq "$h$m"); } }'

Still small enough to be a command-line app, although I bet you could do it smaller in Ruby.
Care to give it a shot?

Nate said...
This comment has been removed by the author.