Advertisement
danchaofan

Euler #93

Dec 27th, 2019
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. from itertools import permutations
  2.  
  3.  
  4. def apply(uno, dos, num):
  5.     if num == 1:
  6.         return uno + dos
  7.     if num == 2:
  8.         return uno - dos
  9.     if num == 3:
  10.         return uno * dos
  11.     if num == 4:
  12.         return uno / dos
  13.  
  14.  
  15. def churn(listing, lineup):
  16.     modifier = 0
  17.     for operator in lineup:
  18.         if operator[1] == 2:
  19.             listing[2 - modifier] = apply(listing[2 - modifier], listing[2 - modifier + 1], operator[0])
  20.             listing[2 - modifier + 1] = ""
  21.             listing.pop(listing.index(""))
  22.         if operator[1] == 1:
  23.             listing[1 - modifier] = apply(listing[1 - modifier], listing[1 - modifier + 1], operator[0])
  24.             listing[1 - modifier + 1] = ""
  25.             listing.pop(listing.index(""))
  26.             modifier += 1
  27.         if operator[1] == 0:
  28.             listing[0] = apply(listing[0], listing[1], operator[0])
  29.             listing[1] = ""
  30.             listing.pop(listing.index(""))
  31.             modifier += 1
  32.     return listing[0]
  33.  
  34.  
  35. operators = []
  36. for first in range(1, 5):
  37.     for second in range(1, 5):
  38.         for third in range(1, 5):
  39.             operators.append([first, second, third])
  40.  
  41. digits = []
  42. for one in range(1, 10):
  43.     for two in range(one + 1, 10):
  44.         for three in range(two + 1, 10):
  45.             for four in range(three + 1, 10):
  46.                 if len([one, two, three, four]) == len({one, two, three, four}):
  47.                     digits.append([one, two, three, four])
  48.  
  49. best, answer = 0, []
  50. for x in digits:
  51.     chain = []
  52.     for y in permutations(x):
  53.         y = list(y)
  54.         for a in operators:
  55.             for b in permutations([0, 1, 2]):
  56.                 try:
  57.                     entry = churn([y[0], y[1], y[2], y[3]], zip(a, b))
  58.                     if entry > 0 and abs(round(entry) - entry) < 0.0000000005 and round(entry) not in chain:
  59.                         chain.append(round(entry))
  60.                 except ZeroDivisionError:
  61.                     pass
  62.     starting, length = 0, 0
  63.     for z in sorted(set(chain)):
  64.         if z != starting + 1:
  65.             if length > best:
  66.                 best = length
  67.                 answer = x
  68.             break
  69.         starting += 1
  70.         length += 1
  71. print(best, answer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement