# 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;
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?

1. Steven Mock

I solved this problem in my head as follows:

Hours 6:00 through 9:00 cannot generate a string of three identical digits because minutes can only range between 00 and 59.

Hours 1:00 through 5:00 can only generate one case of three identical digits each using the last three positions – the zero in the first position of the hour prevents the first three positions from being used and the second position in the hour determines what the last two must be.

Five cases so far.

Hours 10:00 and 12:00 can only generate one case each – the second position determines what the last two must be and it doesn’t match the first position, so the first three can’t be used.

Seven cases so far.

Which leaves hour 11 as a special case – 11:10 through 11:19 generate 10 cases. 11:11 is interesting because this is the only time that the first and last three positions are the same. However, the rules state “three or more identical digits”, so 11:11 should only be counted once.

Which means the number of results is 17.

I think this solution is more of a pattern based approach rather than an exhaustive list and it’s definitely not a program.

So, do I win a no-prize?

Steven

2. Vincent Tan

Hey Steven, you’re almost right! There’s another piece of information in the question. The phrase “during an entire day” is important too. So, uh, no no-prize? *smile*

You know, your method of inspecting each hour to find out how many combinations fit is really good. Your explanation is very clear too. I wish I thought of that when I was struggling… Thanks!

3. Steven Mock

I interpreted this phrase –
“For example, 03:33 is counted as once.” – to mean that 03:33 AM and 03:33 PM only counted as one occurrence.

Bad assumption – I should have hedged my bet.

Bonus question: What if the clock is in 24 hour format?

Steven

4. Vincent Tan

Oh yeah, that phrase _is_ ambiguous. I need to phrase questions better… The way it was phrased in the game was quite clear. I didn’t want to plagiarise though…

The main difference in counting with 24 hour format would be … we have to count _more_ combinations starting with 0 and 2!

I cheated… I modified my program a little. The final count isn’t a lot more than the 12 hour version. Some of the 12-hour combinations simply became a 24-hour version, so the final count isn’t as high.

Basically, we still follow your method and check how many combinations for each hour. There’s just double the number of hours to check.

5. Logic Mike

Of course, unless your digital clock resets itself at every change of minute (mine does not) the same 111 that is displayed at 11:10 will still be on display at 11:19; this would only be one ten-minute long “time” that “3 or more identical digits appear consecutively in a row”; it would seem that some adjustment of wording is required to make the puzzle answer in the game correct; maybe some accuracy was lost in translation?

6. Vincent

Hi Mike,

It’s assumed that you count for every change of the minute, since the digital clock display is correct to the minute.

This means 11:10, 11:11, … 11:18, 11:19 is counted as 10 times with 3 identical digits.

Sometimes, figuring out the unwritten/untold assumptions is part of the puzzle.

7. Jude

Thanks a lot for the SQL. Doesnt help people who dont know what it is. And “go figure it” is a bit arrogant?
For Steven Mock, you are almost there…
Your logic is much better than most. And oesnt need a program.
The question is misleading in that it does not state that they do not apparently recognise 4 digits with 3 leading zero’s i.e. 0001, 0002,etc.
That takes out 9 options.(0001 thru 0009)
Without people who use 12:22 on a 12 hour clock! or 00:00 also.
Check out the web-sites…and all the incorrect answers.
A. 0111, 0222, 0333, 0444, 0555 = 5 so far
B. 1000 = + 1 = 6 so far
C. 1110 thru 1119 = + 10 = 16 so far
So for a 24 hour day on a 12 hour clock it is 32…

Cheers

8. Vincent

Hi Jude, thanks for your input. This is a programming blog. I’ve given a perfectly working piece of code that when compiled and run, will give the answer. If you’re not a programmer, I apologise.

The question also doesn’t need to state that cases with 3 leading zeroes aren’t counted. It’s a puzzle. That information is supposed to be arrived at by the puzzler.

http://en.wikipedia.org/wiki/12-hour_clock