## The confounding digital clock puzzle

Recently, I played Professor Layton and the Curious Village on the Nintendo DS. It’s basically a game filled with puzzle after puzzle for the player to solve.

There are 3D questions testing your visualisation skills (the IQ question on the painted cube for my job interview also came up). There are the logic questions such as “Only 1 of the 4 kids is telling the truth”, and you have to figure out the answer from their statements.

I can solve most of the puzzles in my head. The only time when I needed to write something down was where the puzzle can be distilled into a pair of simultaneous equations. You know the kind, the father is x times as old as the son, and after y number of years, he’d be z times as old as the son, and how old are both of them currently. Or some kind of puzzle with some math variables I need to keep track, but don’t want to do that mentally.

[image by claylib]

Then comes this one puzzle. I was stuck on it for over an hour. I thought, I categorised, I simplified. It’s too mentally taxing to hold all the pieces mentally, and I’m too lazy to write everything out on paper. Here’s the puzzle, paraphrased:

You have a digital clock, displaying the hour and the minute only, with hours in the 12-hour format. How many times during an entire day will 3 or more identical digits appear consecutively in a row? For example, 03:33 is counted as once.

My brother solved it by using Excel to generate all the combinations, and eliminating each combination by inspection. I didn’t want to do that. After some time, I did the only sensible thing. **I wrote a program to solve it.** Muahahahaha…

// any date will do, as long as the time is set // to zero for the hour and minute (and second?) DateTime dt = new DateTime(2008, 10, 1, 0, 0, 0); string s = dt.ToString("yyyyMMdd"); string sTime; char[] ca; int iCount = 0; // while still the same day while (s.Equals("20081001")) { // small hh for 12-hour format sTime = dt.ToString("hhmm"); ca = sTime.ToCharArray(); // check if first three digits are identical // or if the last three digits are identical if ((ca[0] == ca[1] && ca[1] == ca[2]) || (ca[1] == ca[2] && ca[2] == ca[3])) ++iCount; dt = dt.AddMinutes(1); s = dt.ToString("yyyyMMdd"); } Console.WriteLine(iCount);

That took me a couple of minutes to whip up. I added the comments so you can follow the thought process easily. Note the “hhmm” format for 12-hour versus “HHmm” for 24-hour format. 2 seconds to compile and run, and BAM! I got the answer. No, I’m not telling you. Go figure it out yourself.

So I solved the digital clock puzzle with programming. Somehow, it felt like cheating. Anyway, my challenge to you is, **can you solve it in a non-programmatic, non-exhaustive-list-writing way**?