15
\$\begingroup\$

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:

  • addition
  • 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.
\$\endgroup\$
  • 2
    \$\begingroup\$ apps.getpebble.com/en_US/application/5309427924c4582908000046 \$\endgroup\$ – Beta Decay Aug 7 '15 at 17:32
  • 4
    \$\begingroup\$ "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? \$\endgroup\$ – Peter Taylor Aug 7 '15 at 17:45
  • 1
    \$\begingroup\$ Given 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? \$\endgroup\$ – bobbel Aug 7 '15 at 22:05
  • 2
    \$\begingroup\$ Is 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. \$\endgroup\$ – Michael Stern Aug 10 '15 at 17:22
  • 1
    \$\begingroup\$ Michael, 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. \$\endgroup\$ – nobillygreen Aug 10 '15 at 17:30
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.