Sunday Times Teaser 3162 – Flash Cordon and Ming
by Stephen Hogg
Published Sunday April 30 2023 (link)
Housing its priceless Ming collection, the museum’s main hall had eight flashing, motion-sensor alarms. Each alarm’s flash ON and OFF times were settable from 1s to 4s in 0.1s steps. This allowed the flash “duty cycle” (the percentage of one ON+OFF period spent ON) to be altered. To conserve power, all duty cycles were set under 50%. All eight duty cycles were whole numbers, not necessarily all different.
Four alarms were set with consecutive incremental ON times (eg 2.0, 2.1, 2.2, 2.3). One pair of these had equal OFF times, as did the other pair (but a different OFF time from the first pair). Curiously, these two statements also apply to the other four alarms if the words ON and OFF are exchanged.
Give the lowest and highest duty cycles for each set of four alarms.
or more compact
Nice work Frits. Here is my version of your second approach.
A Python related question.
I hoped in the first example I could set dictionaries d1 and d2, it didn’t work.
Does someone know how to replace a variable LHS without having to fill all the elements?
I have tested alarm’s flash ON and OFF times settable from 1 second to 1 million seconds.
I needed to rewrite my program for this. The original method to calculate the dictionary of possible ON/OFF pairs becomes horribly slow. For 100,000 seconds it took almost 19 hours to calculate the dictionary (approx 32 seconds with the new method). Also the product function is only called for a few numbers in the todo list.
It has been verified up to 100,000 deconds that the new program calculates the same dictionary as the old program
Only one additional solution is found with 1 million seconds (early on):
((36, 37, 38, 39), (114, 111, 114, 111)) 1st set: 24 and 26
Nice work Frits. I have not gone through the detail of the calculation of the dictionaries but I like the neat use of the any() clause in the final stage. Getting all the work done in C certainly pays off here!
Another idea would be to build the dictionary at every fourth entry (4-8-12-16 …). A 2nd pass would then only have fill in the gaps k+1, k+2, k+3 if one of d[k] and d[k+4] is non empty.
Most high d_on numbers are empty for a big mx. The d_off dictionary is more evenly distributed.
Jim Randell told me a more efficient way to build the dictionary: for each ON entry check for each percentage 1-49 which whole number OFF entry can occur.
For very big mx values memory is a problem. This can be solved by working with chunks (1.5M entries use approximately 3GB of memory).