Ambiguous Clock
A round clock only has three hands: hour, minute, second. All hands look identical and move continuously. Moreover, there is no number or reference mark so that the “upright position” is unknown. The clock functions the same as a normal 12-hour analogue clock.
Despite the inconvenient design, for most time it is possible to tell the correct time (within a 12-hour cycle) from the clock, just by measuring accurately the angles between the hands. For example, if all three hands coincide, then the time must be 12:00:00.
Nevertheless, there are several moments where the clock shows an ambiguous reading. For example, the following moment could be either 1:30:00 or 7:30:00 (with the clock rotated
). Thus both 1:30:00 and 7:30:00 are ambiguous moments.
Note that even if two hands perfectly coincide, we can still see them as two distinct hands in the same position. Thus for example 3:00:00 and 9:00:00 are not ambiguous moments.
How many ambiguous moments are there within a 12-hour cycle?
link to the problem: https://projecteuler.net/problem=891
Get Saba Wasim’s stories in your inbox
Join Medium for free to get updates from this writer.
Solution:
Step-by-step approach:
Normalization:
- Normalize all times to the form (H,M,S)(H, M, S)(H,M,S) with 0≤H<120 \leq H < 120≤H<12, 0≤M<600 \leq M < 600≤M<60, 0≤S<600 \leq S < 600≤S<60.
Generate Possible Permutations:
- Consider the permutations of (H,M,S)(H, M, S)(H,M,S): there are 3!3!3! permutations for each time instance.
Check for Ambiguity:
- For each time (H,M,S)(H, M, S)(H,M,S), generate the three cyclic permutations (M,S,H)(M, S, H)(M,S,H), (S,H,M)(S, H, M)(S,H,M) and check if they represent distinct valid times within the same 12-hour period.
Calculate the Number of Ambiguous Moments:
- Loop through all possible times in the 12-hour period (43200 seconds).
- For each time, compute the equivalent (H,M,S)(H, M, S)(H,M,S) and generate its permutations.
- Count the times where the permutations fall into a different time slot.
Avoid Overcounting:
- Ensure that each ambiguous moment is counted only once by keeping a record of already seen ambiguous configurations.
Calculation:
We now simulate this step-by-step approach:
def get_time_from_seconds(total_seconds):
hours = (total_seconds // 3600) % 12
minutes = (total_seconds % 3600) // 60
seconds = total_seconds % 60
return (hours, minutes, seconds)
def generate_permutations(time_tuple):
H, M, S = time_tuple
return [
time_tuple, # (H, M, S)
(M, S, H), # (M, S, H)
(S, H, M), # (S, H, M)
]
ambiguous_count = 0
seen_times = set()
for total_seconds in range(12 * 3600):
current_time = get_time_from_seconds(total_seconds)
permutations = generate_permutations(current_time)
unique_times = set(permutations)
if len(unique_times) < len(permutations): # If not all permutations are unique
if not any(time in seen_times for time in unique_times):
ambiguous_count += 1
seen_times.update(unique_times)
print(ambiguous_count)In JS:
function getTimeFromSeconds(totalSeconds) {
const hours = Math.floor(totalSeconds / 3600) % 12;
const minutes = Math.floor(totalSeconds % 3600 / 60);
const seconds = totalSeconds % 60;
return [hours, minutes, seconds];
}
function generatePermutations(timeTuple) {
const [H, M, S] = timeTuple;
return [
timeTuple, // (H, M, S)
[M, S, H], // (M, S, H)
[S, H, M] // (S, H, M)
];
}
let ambiguousCount = 0;
const seenTimes = new Set();
for (let totalSeconds = 0; totalSeconds < 12 * 3600; totalSeconds++) {
const currentTime = getTimeFromSeconds(totalSeconds);
const permutations = generatePermutations(currentTime);
const uniqueTimes = new Set(permutations.map(time => time.toString()));
if (uniqueTimes.size < permutations.length) { // If not all permutations are unique
let isAmbiguous = true;
for (const time of uniqueTimes) {
if (seenTimes.has(time)) {
isAmbiguous = false;
break;
}
}
if (isAmbiguous) {
ambiguousCount++;
}
for (const time of uniqueTimes) {
seenTimes.add(time);
}
}
}
console.log(ambiguousCount);#Code is generated








