## Arithmetic... tock... tick... tock

15

This question brought to you by a game I like to play when stuck in long phone meetings.

Given any two times from a 24 hour clock (from 00:00 to 23:59), how many valid mathematical equations can be generated with all of the times in between using only basic arithmetic operations?

Input: two four digit strings (no colon) representing valid times in a 24 hours cycle.

Examples:

For input=0000, 1300

03:26 produces: "0+3*2=6" and "03*2=6" etc.
11:10 produces quite a few, including: "1*1=1+0" and "1=1=1^0" and  "1=11^0" etc.
12:24 produces: "1/2=2/4" and "1=(2*2)/4" etc.


Valid operations are:

• subtraction
• multiplication
• division (floating point)
• exponentiation
• factorial

Other allowable symbols

• Parentheses
• Equal signs

Shortest code wins.

Notes

• The goal is to find the number of valid expressions between two times, not the number of times that contain a valid expression.
• The two times given as input are included in the range of times.
• You may group the digits in any way possible, so "1223" can be "12 23" or " 1 2 23" or "1 223" etc. etc.
• You may use as many parentheses as needed.
• You may use more than one = sign. For instance, the time 11:11 has the valid expression 1=1=1=1.
• If the first time occurs chronologically after the second time, the range of times should wrap as if crossing into the next day.
• The numbers must remain in their original order- you may not re-order the digits.
• When clustering numbers, zero's may absolutely be the front most digit, in which case, they are ignored ("0303" clustered as "03 03" is just two digits with the value of 3.)
• You MAY NOT use the minus sign as unary negation. Therefore, "12:01" does NOT produce "1-2=-(01)", but DOES produce "1-2=0-1".
• You MAY NOT add decimal points to digits. Therefore, "12:05" does NOT produce "1/2=0.5".
• No chaining of factorials- a digit may be followed by at most one "!", no more, otherwise, many times would have infinite solutions. Ex: "5!" is valid but "5!!" is not valid.

I changed "expression" to "equation" and some other things. Please tell me if these edits conflict with your intent. – lirtosiast – 2015-08-07T17:28:04.037

Looks good, thank you very much. – nobillygreen – 2015-08-07T17:30:11.663

It's not stated explicitly but it appears from the examples that reordering the digits is invalid / does not need to be considered. Is this correct? I assume decimal points are not required either, such as 1/2=0.5 – Level River St – 2015-08-07T17:37:31.293

"The numbers must remain in their original order." Should answer the first question. And you are correct on the second point, no decimal points. I have edited to make that explicit. – nobillygreen – 2015-08-07T17:38:56.617

4"Valid operations include" seems to prevent you from being able to add test cases. It would be a better question if you changed that to "Valid operations are" and added some test cases. It would also be useful to be precise about the endpoints: for input 0000 1300 should equations derived from 0000 and 1300 be included in the count? – Peter Taylor – 2015-08-07T17:45:08.697

1Given digits "1423", do "1+4=2+3", "(1+4)=(2+3)", "(1+4)=2+3" and "1+4=(2+3)" count as one or four equations? And... what are all equations of "0000"? I think of about 100 possibilities, or even more... Could this be? – bobbel – 2015-08-07T22:05:23.417

+1 this is similar to a game my daughter plays :) albeit on a 12 hour clock. – MickyT – 2015-08-08T00:45:44.240

2Is there any restriction on the use of unary operators? Absent such a restriction in the rules, factorial can be applied repeatedly and thus a perfect solution may prove impossible. – Michael Stern – 2015-08-10T17:22:34.927

1Michael, that's a great observation. So for the sake of the puzzle, I think I'll limit it to one factorial per "digit", if that makes sense. Therefor, 5! is valid but 5!! is not valid. – nobillygreen – 2015-08-10T17:30:19.447

1You should post some examples with the number of solutions they generate. Your example doesn't have that. – mbomb007 – 2015-08-10T18:11:35.500

Because the current time only lasts for a minute, you need to write the smallest code possible. – wizzwizz4 – 2016-01-13T19:34:21.220

1

# Python3, 363 chars

Since no answer is given till today, I hand in what I got. Sadly, the try/except block is too fat, I didn't found a way to save chars there. It is really tricky with the nested loops there, not all can be done with list comprehensions I think, but maybe someone can tell me how.

However, I restricted the challenge myself to only use basic math '+-*/' and no parentheses.

a,b = input().split()
r=0
for time in [c for c in range(int(a),int(b)) if c/10%10<6]:
t,*ts='%04d'%time
e=[t]
for d in ts:
e=[(n+o+d,n+d)[o==' '] for o in ' -+*/=' for n in e]
for h in [g for g in [e.split('=') for e in e if '='in e] if len(g)>1]:
for k in h:
try:
if eval(h[0]) != eval(k):
break
except:
break
else:
r+=1
print(r)


My full code (hope something explanatory) on this CodeGolf can be found on my pastebin.