Working on the Digital clock challenge, we've previously established in Part 1, that after recording 121 minutes of brightness levels emitted from a digital clock with a 7-segment display, we can tell the time!
But now let's revisit a more difficult version of this: if we start staring at a wall illuminated by that clock, we don't get absolute brightness levels, but rather only the relative changes from one minute to another. This would probably make it more ambiguous to identify the current point in time. Is it still possible? Let's find out.
It takes some small adjustment to make the Python script from Part 1 to work with relative instead of absolute values. We generate those delta values from the absolute values of a full day:
def get_deltas(data): """ Calculate the deltas (differences) from a given list of numbers. >>> get_deltas([0, 1, 1, 2, 1]) [1, 0, 1, -1] >>> get_deltas([0]) [] """ deltas = [] for index, _ in enumerate(data): if index + 1 < len(data): deltas.append(data[index + 1] - data[index]) return deltas
And now we can use that in our main function:
if __name__ == "__main__": import doctest logging.basicConfig(level=logging.ERROR, format="%(message)s") doctest.testmod() logging.getLogger().setLevel(logging.INFO) # Generate segment count for all minutes of the day all_times = all_times() # And now the relative changes from minute to minute deltas = get_deltas(all_times) # Increase length of observed minutes window, until we get an # unambiguously identified time for i in range(24 * 60): logging.info("Testing if window size of %i minutes is unique." % i) windows = get_windows(deltas, i) if has_unique_elements(windows): logging.info("Windows of size %i provide unique combinations." % i) break
When running it, we quickly get a conclusion:
Windows of size 600 provide unique combinations.
So, if we observe 600 minutes (10 hours!) of brightness changes caused by the changes of the clock numbers, we can deduce which time it is!
That sounds absurdly long. Of 1440 minutes in a day, we need to wait 600 minutes in order to find a truly unique sequence of changes?
If you think about the inherent order that is present in those values
it makes sense. The changes of the 2 digits that represent the minutes, from :00
to :59
repeat every hour,
so any sequence below 60 minutes will be present at least 24 times during the day. We need to rely on the hour
digits in order to form a unique sequence. And they don't change very often (well, once per hour), so now it makes
sense that we need to make the time window quite long in order to ensure a unique sequence of changes. And it is not
by accident that the result, 600 minutes, is a whole-number of hours.