Advertisement
Guest User

riddler.py

a guest
Dec 19th, 2017
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 KB | None | 0 0
  1.  
  2. import itertools
  3. import sys
  4. import re
  5.  
  6. def noEquals(operatorList):
  7.     noEquals = True
  8.     for operator in operatorList:
  9.         if(operator == "5"):
  10.             return False
  11.     return True
  12.  
  13. digits = int(sys.argv[1])
  14. operatorLists = []
  15. validDigits = '012345'
  16. for num in itertools.product(validDigits, repeat=digits-1):
  17.     s = ''.join(num)
  18.     operatorLists.append(s)
  19. attempts = 0
  20. numValid = 0
  21. for a in range(0, 10**digits):
  22.     if(a%1000==0):
  23.         print(a)
  24.     textNum = str(a)
  25.     while(len(textNum)<digits):
  26.         textNum = "0" + textNum
  27.  
  28.     isValid = False
  29.     for operatorList in operatorLists:
  30.         if(noEquals(operatorList) == True):
  31.             continue
  32.  
  33.         atEquals = False
  34.         iterLocation = 0
  35.         expressionList = []
  36.         expression = textNum[iterLocation]
  37.         while(iterLocation < digits):
  38.             if(iterLocation == digits-1):
  39.                 expressionList.append(expression)
  40.                 break    
  41.             if(operatorList[iterLocation] == "5"):
  42.                 expressionList.append(expression)
  43.                 expression = textNum[iterLocation+1]
  44.             if(operatorList[iterLocation] == "0"):
  45.                 expression = expression + "+" + textNum[iterLocation+1]
  46.             if(operatorList[iterLocation] == "1"):
  47.                 expression = expression + "-" + textNum[iterLocation+1]
  48.             if(operatorList[iterLocation] == "2"):
  49.                 expression = expression + "*" + textNum[iterLocation+1]
  50.             if(operatorList[iterLocation] == "3"):
  51.                 expression = expression + "/" + textNum[iterLocation+1]
  52.             if(operatorList[iterLocation] == "4"):
  53.                 expression = expression + "**" + textNum[iterLocation+1]
  54.             if(operatorList[iterLocation] == "6"):
  55.                 expression = expression + "%" + textNum[iterLocation+1]
  56.            
  57.             iterLocation += 1
  58.  
  59.         isValid = True
  60.  
  61.         for c in range(0, len(expressionList)-1):
  62.  
  63. #The following 24 lines are optional to include if you have a slow processor - they skip over the bigger exponentiations.
  64.             try:
  65.                 if(expressionList[c].count("*") >= 4):
  66.                     digitsOnly = re.sub("[^0-9]", "", expressionList[c])
  67.                     numAbove = 0
  68.                     exponentSum = 0
  69.                     for a in range(0, len(digitsOnly)):
  70.                         if(int(digitsOnly[a]) >= 2):
  71.                             numAbove += 1
  72.                         exponentSum += int(digitsOnly[a])
  73.                     if(numAbove >= 3 and exponentSum >= 15):
  74.                         isValid = False
  75.                         continue
  76.                 if(expressionList[c+1].count("*") >= 4):
  77.                     digitsOnly = re.sub("[^0-9]", "", expressionList[c+1])
  78.                     numAbove = 0
  79.                     exponentSum = 0
  80.                     for a in range(0, len(digitsOnly)):
  81.                         if(int(digitsOnly[a]) >= 2):
  82.                             numAbove += 1
  83.                         exponentSum += int(digitsOnly[a])
  84.                     if(numAbove >= 3 and exponentSum >= 15):
  85.                         isValid = False
  86.                         continue
  87. #End optional code section
  88.  
  89.                 if(eval(expressionList[c]) != eval(expressionList[c+1])):
  90.                     isValid = False
  91.             except (OverflowError, ZeroDivisionError):
  92.                 isValid = False
  93.                 break
  94.         if(isValid == True):
  95.             break
  96.     attempts += 1
  97.     if(isValid == True):
  98.         numValid += 1
  99.     if(isValid == False):
  100.         print(textNum)
  101.    
  102. print("Number of digit strings analyzed: " + str(attempts))
  103. print("Number of strings with solutions: " + str(numValid))
  104. print("Percent of strings with solutions: " + str(numValid/attempts))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement