Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. # opcode computer
  2.  
  3. ints_raw = [1, 0, 0, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 1, 6, 19, 1, 19, 5, 23, 2, 13, 23, 27, 1, 10, 27, 31, 2,
  4.             6, 31, 35, 1, 9, 35, 39, 2, 10, 39, 43, 1, 43, 9, 47, 1, 47, 9, 51, 2, 10, 51, 55, 1, 55, 9, 59, 1, 59, 5,
  5.             63, 1, 63, 6, 67, 2, 6, 67, 71, 2, 10, 71, 75, 1, 75, 5, 79, 1, 9, 79, 83, 2, 83, 10, 87, 1, 87, 6, 91, 1,
  6.             13, 91, 95, 2, 10, 95, 99, 1, 99, 6, 103, 2, 13, 103, 107, 1, 107, 2, 111, 1, 111, 9, 0, 99, 2, 14, 0, 0]
  7. ints_reset = [1, 0, 0, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 1, 6, 19, 1, 19, 5, 23, 2, 13, 23, 27, 1, 10, 27, 31, 2,
  8.             6, 31, 35, 1, 9, 35, 39, 2, 10, 39, 43, 1, 43, 9, 47, 1, 47, 9, 51, 2, 10, 51, 55, 1, 55, 9, 59, 1, 59, 5,
  9.             63, 1, 63, 6, 67, 2, 6, 67, 71, 2, 10, 71, 75, 1, 75, 5, 79, 1, 9, 79, 83, 2, 83, 10, 87, 1, 87, 6, 91, 1,
  10.             13, 91, 95, 2, 10, 95, 99, 1, 99, 6, 103, 2, 13, 103, 107, 1, 107, 2, 111, 1, 111, 9, 0, 99, 2, 14, 0, 0]
  11. start_index = 0
  12. segment_count = int(len(ints_raw) // 4)
  13. task_counter = 0
  14. address_0 = 1
  15. input_required = ()
  16. input_found = False
  17.  
  18.  
  19. # Opcode 1 Function (addition)
  20. def opcode_1(segment):
  21.     index_1 = int(segment[1])
  22.     index_2 = int(segment[2])
  23.     index_3 = int(segment[3])
  24.  
  25.     output = ints_raw[index_1] + ints_raw[index_2]
  26.     ints_raw[index_3] = output
  27.  
  28.  
  29. # Opcode 2 Function (multiplication)
  30. def opcode_2(segment):
  31.     index_1 = int(segment[1])
  32.     index_2 = int(segment[2])
  33.     index_3 = int(segment[3])
  34.  
  35.     output = ints_raw[index_1] * ints_raw[index_2]
  36.     ints_raw[index_3] = output
  37.  
  38.  
  39. # segment_splitter splits the segment out from the inits_raw list
  40. def segment_splitter():
  41.     opcodes = ints_raw[int(start_index):int(start_index + 4)]
  42.     return opcodes
  43.  
  44.  
  45. # check_opcode checks the first value in the segment, to determine which operation to run
  46. def check_opcode(segment):
  47.     index_0 = segment[0]
  48.     if index_0 == 1:
  49.         opcode_1(segment)
  50.     elif index_0 == 2:
  51.         opcode_2(segment)
  52.     elif index_0 == 99:
  53.         print("Program has finished.", index_0)
  54.     else:
  55.         print('Something went wrong, Encountered an unknown Opcode.', index_0)
  56.  
  57.  
  58. # loop through the two inputs producing the output for each pair of inputs (will add condition for answer later)
  59. noun = 1
  60. verb = 1
  61.  
  62.  
  63. while verb <= 99:
  64.     noun += 1
  65.     if noun == 99:
  66.         noun = 1
  67.         verb += 1
  68.     ints_raw[1] = noun
  69.     ints_raw[2] = verb
  70.     while task_counter < segment_count:
  71.         check_opcode(segment_splitter())
  72.         start_index += 4
  73.         task_counter += 1
  74.     start_index = 0
  75.     task_counter = 0
  76.     if ints_raw[0] == 19690720:
  77.         input_required = (noun, verb)
  78.     ints_raw = ints_reset.copy()
  79.  
  80. print(input_required)
  81. print(100 * 95 + 7)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement