19
\$\begingroup\$

In my Economics class, my friends and I like to come up with ways to rearrange the digits in the date (in MM/DD/YY) format to create a valid mathematical equation. For the most part, we are allowed to use addition, subtraction, multiplication, division, parentheses, and exponentiation in addition to concatenation.

Your program should do something similar. The program should import the current date and insert operators to print an expression according to the following rules.

  • The digits MUST be used in order. Rearrangement of digits is not allowed.
  • The resulting expression must be mathematically accurate.
  • Addition, subtraction, multiplication, division, exponentiation, and use of parentheses is allowed. So is concatenation of digits. However, not all operations are necessary. You cannot use a subtraction sign to make a digit negative (like -1+1+11=10 on November 11, 2010).
  • The program must run in 60 seconds on a standard machine.

For example, this challenge was written on November 10, 2015. The program would interpret this as 11/10/15. A sample output would be (1+1)/10=1/5.


Bonuses

You may multiply the number of bytes in your code by 0.9 for each one of the following your program supports.

  • The program prints all possible expressions that can be formed, separated by newlines. Multiply by an additional 0.95 if the expressions are listed in increasing order of additional symbols.
  • The program also works for MM/DD/YYYY dates, printing a possibility with the first two digits of the year in addition to the possibility without. If this bonus is combined with the first bonus, all possibilities with the first two digits of the year must be printed.
  • The program also prints an equation for when there are multiple equalities (for example, on November 11, 2011, 1=1=1=1=1=1 would be printed, in addition to possibilities such as 1*1=1=1=1=1, 1*1*1=1=1=1, and 1*1*1*1=1=1. All such cases must be printed for the first bonus to be achieved.
  • The program supports conversion to bases between 2 and 16. Note that if the base is not 10, all numbers in the expression must be written in the same base, and (Base b) must be written after the expression (with b replaced accordingly).

This is code golf, so standard rules apply. Shortest code in bytes wins.

\$\endgroup\$
  • 1
    \$\begingroup\$ What operations are allowed? \$\endgroup\$ – anOKsquirrel Nov 10 '15 at 17:29
  • 1
    \$\begingroup\$ @FryAmTheEggman Is there enough similarity to call this a duplicate? I didn't think so because this challenge doesn't use exclusively one digit and does not have a specific RHS in mind (only equality). \$\endgroup\$ – Arcturus Nov 10 '15 at 17:38
  • 17
    \$\begingroup\$ DD/MM/YYYY > MM/DD/YYYY. \$\endgroup\$ – orlp Nov 10 '15 at 18:09
  • 3
    \$\begingroup\$ I think you want to use equation in your question where you wrote expression (an expression is just one side of the equation, and then your question doesn't really make sense). \$\endgroup\$ – Paŭlo Ebermann Nov 10 '15 at 19:36
  • 1
    \$\begingroup\$ Is this proven possible for any given date? \$\endgroup\$ – Zach Gates Apr 4 '16 at 5:08
6
\$\begingroup\$

Python 3, 424 420 369 363 bytes

import time as t
r=range
x=len
d=list(t.strftime('%m%d%y'))
o=([[x,x+'(',x+')']for x in ['']+"+ - == * / **".split()])
n=[]
for l in o:
    n=l+n
o=n
for p in r(x(o)**(x(d)-1)):
    e=''
    for i in r(x(d)-1):
        e+=str(d[i])+o[(p//(x(o)**i))%x(o)]
    e+=str(d[-1])
    try:
        if eval(e)and e.find('=')!=-1:
            print(e.replace('==','=').replace('**','^'))
            break
    except:pass

Brute-forces all possible combinations of operations in the numbers and stops when it finds one.

EDIT: Saved 4 bytes thanks to @NoOneIsHere

EDIT 2: Saved 51(!) bytes thanks to @ValueInk

\$\endgroup\$
  • 1
    \$\begingroup\$ Hello, and welcome to PPCG! You can inline the except:pass, and remove the space in [ (p//(len(o)**i))%len(o)]. \$\endgroup\$ – NoOneIsHere Aug 5 '16 at 20:24
  • \$\begingroup\$ If you're importing division from __future__, would upgrading to Python 3 work better for your situation? Also, I don't understand why you reverse o when you're building that list of operators-with-parens you have. \$\endgroup\$ – Value Ink Aug 6 '16 at 3:02
  • \$\begingroup\$ @ValueInk Yeah, I could probably change it to python 3 and save quite a few bytes. When I started doing the challenge, I wasn't focused on golfing it at all, so it can definitely still be made shorter. Also, since the program is brute forcing all combinations until it finds one, speed is an issue, and I found that it tends to work faster if you use the reverse of o. I'll probably golf this down a bit more to make it a serious contender. \$\endgroup\$ – Theo Aug 6 '16 at 3:08
  • \$\begingroup\$ o=([[x,x+'(',x+')']for x in",+,-,==,*,/,**".split(',')]) for 2 bytes \$\endgroup\$ – Jonathan Allan Aug 7 '16 at 20:58
  • 1
    \$\begingroup\$ @JonathanAllan Huh. Thanks for pointing these out. Ill do a rework of the code when I have time (probably tomorrow) \$\endgroup\$ – Theo Aug 8 '16 at 3:03

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.