Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. from config import file_path
  2.  
  3. def read_file():
  4. with open(file_path) as f:
  5. lines = f.readlines()
  6. return lines
  7.  
  8. def add_int(int_program, program_counter, first_par, second_par, third_par):
  9. if(first_par):
  10. first_val = int(int_program[program_counter + 1])
  11. else:
  12. first_val = int_program[int(int_program[program_counter + 1])]
  13. if(second_par):
  14. second_val = int(int_program[program_counter + 2])
  15. else:
  16. second_val = int_program[int(int_program[program_counter + 2])]
  17. if(third_par):
  18. adress = int(int_program[program_counter + 3])
  19. else:
  20. adress = int(int_program[program_counter + 3])
  21. int_program[int(adress)] = int(first_val) + int(second_val)
  22. return int_program
  23.  
  24. def mult_int(int_program, program_counter, first_par, second_par, third_par):
  25. if(first_par):
  26. first_val = int(int_program[program_counter + 1])
  27. else:
  28. first_val = int_program[int(int_program[program_counter + 1])]
  29. if(second_par):
  30. second_val = int(int_program[program_counter + 2])
  31. else:
  32. second_val = int_program[int(int_program[program_counter + 2])]
  33. if(third_par):
  34. adress = int(int_program[program_counter + 3])
  35. else:
  36. adress = int(int_program[program_counter + 3])
  37. int_program[int(adress)] = int(first_val) * int(second_val)
  38. return int_program
  39.  
  40. def read_op(int_program, program_counter, first_par, second_par, third_par):
  41. if(first_par):
  42. read_val = int_program[program_counter + 1]
  43. else:
  44. read_val = int_program[int(int_program[program_counter + 1])]
  45. #print("R: ", read_val)
  46. return read_val
  47.  
  48. def write_op(int_program, program_counter, input, first_par, second_par, third_par):
  49. int_program[int(int_program[program_counter + 1])] = input
  50. return int_program
  51.  
  52. def jump_true(int_program, program_counter, first_par, second_par):
  53. if(first_par):
  54. first_val = int(int_program[program_counter + 1])
  55. else:
  56. first_val = int_program[int(int_program[program_counter + 1])]
  57. if(second_par):
  58. second_val = int(int_program[program_counter + 2])
  59. else:
  60. second_val = int_program[int(int_program[program_counter + 2])]
  61. if(int(first_val) > 0):
  62. program_counter = second_val
  63. return program_counter
  64. else:
  65. return program_counter + 3
  66.  
  67. def jump_false(int_program, program_counter, first_par, second_par, third_par):
  68. if(first_par):
  69. first_val = int(int_program[program_counter + 1])
  70. else:
  71. first_val = int_program[int(int_program[program_counter + 1])]
  72. if(second_par):
  73. second_val = int(int_program[program_counter + 2])
  74. else:
  75. second_val = int_program[int(int_program[program_counter + 2])]
  76. if(int(first_val) == 0):
  77. program_counter = second_val
  78. return program_counter
  79. else:
  80. return program_counter + 3
  81.  
  82. def less_than(int_program, program_counter, first_par, second_par, third_par):
  83. if(first_par):
  84. first_val = int(int_program[program_counter + 1])
  85. else:
  86. first_val = int_program[int(int_program[program_counter + 1])]
  87. if(second_par):
  88. second_val = int(int_program[program_counter + 2])
  89. else:
  90. second_val = int_program[int(int_program[program_counter + 2])]
  91. if(third_par):
  92. adress = int(int_program[program_counter + 3])
  93. else:
  94. adress = int(int_program[program_counter + 3])
  95. if(int(first_val) < int(second_val)):
  96. int_program[adress] = 1
  97. else:
  98. int_program[adress] = 0
  99. return int_program
  100.  
  101. def equal(int_program, program_counter, first_par, second_par, third_par):
  102. if(first_par):
  103. first_val = int(int_program[program_counter + 1])
  104. else:
  105. first_val = int_program[int(int_program[program_counter + 1])]
  106. if(second_par):
  107. second_val = int(int_program[program_counter + 2])
  108. else:
  109. second_val = int_program[int(int_program[program_counter + 2])]
  110. if(third_par):
  111. adress = int(int_program[program_counter + 3])
  112. else:
  113. adress = int(int_program[program_counter + 3])
  114. if(int(first_val) == int(second_val)):
  115. int_program[adress] = 1
  116. else:
  117. int_program[adress] = 0
  118. return int_program
  119.  
  120.  
  121. def run_int_program(int_program, sequence_num, read_val):
  122. program_counter = 0
  123. is_squence = True
  124. while(1):
  125. index = int(int_program[program_counter])
  126. first_par = False
  127. second_par = False
  128. third_par = False
  129. if(len(str(index)) > 1):
  130. if(len(str(index)) == 5):
  131. if(str(index)[0] == '1'):
  132. third_par = True
  133. if(str(index)[1] == '1'):
  134. second_par = True
  135. if(str(index)[2] == '1'):
  136. first_par = True
  137. index = str(index)[3] + str(index)[4]
  138. index = int(index)
  139. elif(len(str(index)) == 4):
  140. if(str(index)[1] == '1'):
  141. first_par = True
  142. if(str(index)[0] == '1'):
  143. second_par = True
  144. index = str(index)[len(str(index)) - 2] + str(index)[len(str(index)) - 1]
  145. index = int(index)
  146. elif(len(str(index)) == 3):
  147. if(str(index)[0] == '1'):
  148. first_par = True
  149. index = str(index)[len(str(index)) - 2] + str(index)[len(str(index)) - 1]
  150. index = int(index)
  151. if(index == 1):
  152. int_program = add_int(int_program, program_counter, first_par, second_par, third_par)
  153. program_counter = program_counter + 4
  154. elif(index == 2):
  155. int_program = mult_int(int_program, program_counter, first_par, second_par, third_par)
  156. program_counter = program_counter + 4
  157. elif(index == 3):
  158. #print(is_squence, sequence_num, read_val)
  159. if(is_squence):
  160. int_program = write_op(int_program, program_counter, sequence_num, first_par, second_par, third_par)
  161. is_squence = False
  162. else:
  163. int_program = write_op(int_program, program_counter, read_val, first_par, second_par, third_par)
  164. is_squence = True
  165. program_counter = program_counter + 2
  166. elif(index == 4):
  167. read_val = read_op(int_program, program_counter, first_par, second_par, third_par)
  168. program_counter = program_counter + 2
  169. elif(index == 5):
  170. program_counter = jump_true(int_program, program_counter, first_par, second_par)
  171. program_counter = int(program_counter)
  172. elif(index == 6):
  173. program_counter = jump_false(int_program, program_counter, first_par, second_par, third_par)
  174. program_counter = int(program_counter)
  175. elif(index == 7):
  176. int_program = less_than(int_program, program_counter, first_par, second_par, third_par)
  177. program_counter = program_counter + 4
  178. elif(index == 8):
  179. int_program = equal(int_program, program_counter, first_par, second_par, third_par)
  180. program_counter = program_counter + 4
  181.  
  182. elif(index == 99):
  183. return read_val
  184. else:
  185. print("Wtf", index)
  186. return
  187.  
  188. def main():
  189. int_program = read_file()
  190. int_program = int_program[0]
  191. int_program = int_program.split(",")
  192. max_val = 0
  193. r_val = 0
  194. seq = []
  195. for one in range(0,5):
  196. for two in range(0,5):
  197. for three in range(0,5):
  198. for four in range(0,5):
  199. for five in range(0,5):
  200. sequence = [one,two,three,four,five]
  201. r_val = 0
  202. for i in sequence:
  203.  
  204. r_val = run_int_program(int_program, i, r_val)
  205. if(r_val > max_val):
  206.  
  207. print(r_val, ">", max_val)
  208. max_val = r_val
  209. seq = sequence
  210.  
  211. print("Max Value: ", max_val, seq)
  212.  
  213.  
  214. if __name__== "__main__":
  215. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement