Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. # input pattern we are looking for and helper information about it
  2. pattern = "652601"
  3. patternInt = int( pattern)
  4. patternDigitsMagnitude = 10**len( str( pattern))
  5.  
  6. # start from the [ 3  7  1 [0](1) 0 ] as initial position
  7. # to avoid checking an additional condition inside the loop
  8. recipes = [ 3, 7, 1, 0, 1, 0]
  9. elf1CNodeIdx = 4
  10. elf2CNodeIdx = 3
  11.  
  12. currentRunningPattern = 371010 % patternDigitsMagnitude
  13. while True:
  14.     lenRecipes = len( recipes)
  15.     #make recipes
  16.     elf1CNodeVal = recipes[elf1CNodeIdx]
  17.     elf2CNodeVal = recipes[elf2CNodeIdx]
  18.     newRecipesSum = elf1CNodeVal + elf2CNodeVal
  19.     if newRecipesSum > 9: # if the sum is ge 10 adds an additional second node
  20.         recipes.append( 1) # node we are adding is always 1
  21.         newRecipesSum -= 10
  22.         #pattern scan
  23.         currentRunningPattern *= 10
  24.         currentRunningPattern += 1 # node we are adding is always 1
  25.         currentRunningPattern %= patternDigitsMagnitude
  26.         if currentRunningPattern == patternInt:
  27.             print( "The pattern occurs at: ", len( recipes) - len( pattern))
  28.             break
  29.     recipes.append( newRecipesSum) # always add at least one node
  30.     #pattern scan
  31.     currentRunningPattern *= 10
  32.     currentRunningPattern += newRecipesSum
  33.     currentRunningPattern %= patternDigitsMagnitude
  34.     if currentRunningPattern == patternInt:
  35.         print( "Pattern occurs at position: ", len( recipes) - len( str( pattern)))
  36.         break
  37.     #advance elves
  38.     elf1CNodeIdx = (elf1CNodeIdx + (elf1CNodeVal + 1)) % len( recipes)
  39.     elf2CNodeIdx = (elf2CNodeIdx + (elf2CNodeVal + 1)) % len( recipes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement