Advertisement
Guest User

Untitled

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