Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.73 KB | None | 0 0
  1. class IPC2:
  2.     def __init__(self, input_data):
  3.         self.input_data = input_data.copy()
  4.  
  5.     def run(self, program_input):
  6.         output = []
  7.         data = self.input_data.copy()
  8.         i_pointer = 0
  9.         while i_pointer < len(data):
  10.             operation = data[i_pointer]
  11.             # op_code = operation % 100
  12.             modes, op_code = divmod(operation, 100)
  13.             # powinno byc jeszcze to, ale bez tego tez dziala
  14.             # modes = int(str(modes), base=2)
  15.  
  16.             def get_idx(arg_no):
  17.                 # return i_pointer + 1 + arg_no if (operation // (100 * pow(10, arg_no))) % 10 == 1 else data[i_pointer + 1 + arg_no]
  18.                 return i_pointer + 1 + arg_no if modes & (1 << arg_no) else data[i_pointer + 1 + arg_no]
  19.  
  20.             if op_code == 1:
  21.                 data[get_idx(2)] = data[get_idx(0)] + data[get_idx(1)]
  22.                 i_pointer += 4
  23.             elif op_code == 2:
  24.                 data[get_idx(2)] = data[get_idx(0)] * data[get_idx(1)]
  25.                 i_pointer += 4
  26.             elif op_code == 3:
  27.                 data[get_idx(0)] = program_input
  28.                 i_pointer += 2
  29.             elif op_code == 4:
  30.                 output.append(data[get_idx(0)])
  31.                 i_pointer += 2
  32.             elif op_code == 5:
  33.                 i_pointer = data[get_idx(1)] if data[get_idx(0)] != 0 else i_pointer + 3
  34.             elif op_code == 6:
  35.                 i_pointer = data[get_idx(1)] if data[get_idx(0)] == 0 else i_pointer + 3
  36.             elif op_code == 7:
  37.                 data[get_idx(2)] = (1 if data[get_idx(0)] < data[get_idx(1)] else 0)
  38.                 i_pointer += 4
  39.             elif op_code == 8:
  40.                 data[get_idx(2)] = (1 if data[get_idx(0)] == data[get_idx(1)] else 0)
  41.                 i_pointer += 4
  42.             elif op_code == 99:
  43.                 return output
  44.             else:
  45.                 raise ValueError("Bad operation!")
  46.         return output
  47.  
  48.  
  49. def main():
  50.     INTCODE_RAW = "3,225,1,225,6,6,1100,1,238,225,104,0,1101,37,61,225,101,34,121,224,1001,224,-49,224,4,224,102,8,223,223,1001,224,6,224,1,224,223,223,1101,67,29,225,1,14,65,224,101,-124,224,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1102,63,20,225,1102,27,15,225,1102,18,79,224,101,-1422,224,224,4,224,102,8,223,223,1001,224,1,224,1,223,224,223,1102,20,44,225,1001,69,5,224,101,-32,224,224,4,224,1002,223,8,223,101,1,224,224,1,223,224,223,1102,15,10,225,1101,6,70,225,102,86,40,224,101,-2494,224,224,4,224,1002,223,8,223,101,6,224,224,1,223,224,223,1102,25,15,225,1101,40,67,224,1001,224,-107,224,4,224,102,8,223,223,101,1,224,224,1,223,224,223,2,126,95,224,101,-1400,224,224,4,224,1002,223,8,223,1001,224,3,224,1,223,224,223,1002,151,84,224,101,-2100,224,224,4,224,102,8,223,223,101,6,224,224,1,224,223,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,108,677,677,224,1002,223,2,223,1006,224,329,101,1,223,223,1107,677,226,224,102,2,223,223,1006,224,344,101,1,223,223,8,677,677,224,1002,223,2,223,1006,224,359,101,1,223,223,1008,677,677,224,1002,223,2,223,1006,224,374,101,1,223,223,7,226,677,224,1002,223,2,223,1006,224,389,1001,223,1,223,1007,677,677,224,1002,223,2,223,1006,224,404,1001,223,1,223,7,677,677,224,1002,223,2,223,1006,224,419,1001,223,1,223,1008,677,226,224,1002,223,2,223,1005,224,434,1001,223,1,223,1107,226,677,224,102,2,223,223,1005,224,449,1001,223,1,223,1008,226,226,224,1002,223,2,223,1006,224,464,1001,223,1,223,1108,677,677,224,102,2,223,223,1006,224,479,101,1,223,223,1108,226,677,224,1002,223,2,223,1006,224,494,1001,223,1,223,107,226,226,224,1002,223,2,223,1006,224,509,1001,223,1,223,8,226,677,224,102,2,223,223,1006,224,524,1001,223,1,223,1007,226,226,224,1002,223,2,223,1006,224,539,1001,223,1,223,107,677,677,224,1002,223,2,223,1006,224,554,1001,223,1,223,1107,226,226,224,102,2,223,223,1005,224,569,101,1,223,223,1108,677,226,224,1002,223,2,223,1006,224,584,1001,223,1,223,1007,677,226,224,1002,223,2,223,1005,224,599,101,1,223,223,107,226,677,224,102,2,223,223,1005,224,614,1001,223,1,223,108,226,226,224,1002,223,2,223,1005,224,629,101,1,223,223,7,677,226,224,102,2,223,223,1005,224,644,101,1,223,223,8,677,226,224,102,2,223,223,1006,224,659,1001,223,1,223,108,677,226,224,102,2,223,223,1005,224,674,1001,223,1,223,4,223,99,226"
  51.     input_data = [int(i) for i in INTCODE_RAW.split(',')]
  52.     ipc = IPC2(input_data)
  53.     # Part 1
  54.     res = ipc.run(1)[-1]
  55.     print('PART 1: %i' % res)
  56.     # Part 2
  57.     res = ipc.run(5)[-1]
  58.     print('PART 1: %i' % res)
  59.  
  60.  
  61. if __name__ == '__main__':
  62.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement