Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. from math import factorial
  2. global list_previous_jug_states
  3. list_previous_jug_states = []
  4. global list_running_count
  5. list_running_count = []
  6. global list_running_index
  7. list_running_index = []
  8. global list_volumes
  9. list_volumes = []
  10. global list_jug_max
  11. list_jug_max = []
  12.  
  13. class CreateJugs:
  14. #Create a new jug instance
  15. def __init__ (self,jug_name,jug_max):
  16. self.name = jug_name
  17. self.max = jug_max
  18.  
  19. list_jug_max.append(self)
  20.  
  21. @property
  22. def jug_max (self):
  23. return self.max
  24.  
  25.  
  26. def set_fill_states (number_jugs, jug_max):
  27. #Create a list of binary starting states (e.g: 0,0,0,0....1,1,1,0 where 1 = MAX and 0 = empty)
  28. global list_binary_states
  29. list_binary_states = []
  30. for i in range (1<<number_jugs):
  31. binary_state =bin(i)[2:]
  32. binary_state ='0'*(number_jugs-len(binary_state))+binary_state
  33. list_binary_states.append(binary_state)
  34. list_binary_states = list_binary_states[0:len(list_binary_states)-1]
  35.  
  36. #Create lists to hold previous states, running count for each jug, running index of previous positions
  37. #Running count: if start position is (binary): 1,1,0 that = 2
  38. #Running count: start 0,0,1 = 1
  39.  
  40.  
  41.  
  42. #Sort list by number of 1s
  43. new_list = []
  44. for x in range (number_jugs):
  45. for item in list_binary_states:
  46. if item.count('1') == x:
  47. new_list.append(item)
  48. list_running_count.append(x)
  49. #Copy list back to origina function
  50. list_binary_states = new_list[:]
  51.  
  52. #Now print all possible starting oreintations
  53. for n in range (len(list_binary_states)):
  54. jug_binary_state = list_binary_states[int(n)]
  55. jug_state = []
  56. for x in range (number_jugs):
  57. if int(jug_binary_state[x]) == 1:
  58. jug_state.append(list_jug_max[x].max)
  59. else:
  60. jug_state.append (0)
  61. list_previous_jug_states.append(jug_state)
  62. list_running_index.append([n])
  63.  
  64. def make_moves (jug_state,
  65. running_total, running_index,
  66. target_volume, number_jugs):
  67. for from_jug in range (number_jugs):
  68. from_jug_max = list_jug_max[from_jug].jug_max
  69. from_jug_state = jug_state[from_jug]
  70.  
  71. for to_jug in range (number_jugs):
  72. if to_jug == from_jug: continue
  73.  
  74. to_jug_max = list_jug_max[to_jug].jug_max
  75. to_jug_state = jug_state[to_jug]
  76.  
  77. #Empty from_jug, ignore to_jug
  78. new_jug_state = jug_state [:]
  79. new_jug_state[from_jug] = 0
  80. if new_jug_state not in list_previous_jug_states:
  81. list_previous_jug_states.append(new_jug_state)
  82. list_running_count.append (running_total+1)
  83. new_index = [len(list_previous_jug_states)-1]
  84. list_running_index.append (running_index + new_index)
  85. #Fill from_jug, ignore to_jug
  86. new_jug_state = jug_state [:]
  87. new_jug_state[from_jug] = from_jug_max
  88. if new_jug_state not in list_previous_jug_states:
  89. list_previous_jug_states.append(new_jug_state)
  90. list_running_count.append (running_total+1)
  91. new_index = [len(list_previous_jug_states)-1]
  92. list_running_index.append (running_index + new_index)
  93.  
  94. #Move as much from from_jug to to_jug
  95. if to_jug_state == to_jug_max: continue
  96. if from_jug_state == 0: continue
  97. if from_jug_state < (to_jug_max-to_jug_state):
  98. new_jug_state = jug_state [:]
  99. new_jug_state[from_jug] = 0
  100. new_jug_state[to_jug] = to_jug_state + from_jug_state
  101. else:
  102. amount_transfer = to_jug_max - to_jug_state
  103. new_jug_state = jug_state [:]
  104. new_jug_state[from_jug] = from_jug_state - amount_transfer
  105. new_jug_state[to_jug] = to_jug_state + amount_transfer
  106.  
  107. if new_jug_state not in list_previous_jug_states:
  108. list_previous_jug_states.append(new_jug_state)
  109. list_running_count.append (running_total+1)
  110. new_index = [len(list_previous_jug_states)-1]
  111. list_running_index.append (running_index + new_index)
  112.  
  113. for jug in new_jug_state:
  114. if jug == target_volume:
  115. print ("Target reached in ", running_total + 1, "steps")
  116. for item in running_index:
  117. print (list_previous_jug_states[item])
  118. print (new_jug_state)
  119. return True
  120.  
  121.  
  122. return False, 0
  123.  
  124. if __name__ == "__main__":
  125. number_jugs = int(input("Please enter the number of jugs you have: "))
  126. #Set jug volumes
  127. for i in range (number_jugs):
  128. jug_volume = int(input(f"Please enter the volume of jug {i+1}: "))
  129. list_volumes.append(jug_volume)
  130. #Set target volume
  131. target_volume = int(input("Please enter the target volume: "))
  132. #Sort jugs in size order
  133. list_volumes.sort(reverse=True)
  134. #Create object instances
  135. for i in range (number_jugs):
  136. jug_name = "Jug" + str(i+1)
  137. CreateJugs (jug_name, list_volumes[i])
  138. # set_fill_states(number_jugs) #Set the fill states
  139. set_fill_states(number_jugs,list_volumes)
  140. #Continue iterating through jug_previous_state
  141. for item in list_previous_jug_states:
  142. jug_state = item
  143.  
  144. index = list_previous_jug_states.index(item)
  145. running_total = list_running_count [index]
  146. running_index = list_running_index [index]
  147. is_destination = make_moves (jug_state,
  148. running_total,
  149. running_index,
  150. target_volume,
  151. number_jugs)
  152.  
  153. if is_destination == True:
  154. print ("=====")
  155. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement