Advertisement
VSZM

Untitled

Dec 6th, 2019
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.19 KB | None | 0 0
  1. from abc import ABC, abstractmethod
  2. from typing import List
  3. import sys
  4.  
  5. POSITION_MODE = 0
  6. VALUE_MODE = 1
  7.  
  8. class InstructionBase(ABC):
  9.  
  10.     def __init__(self, operand1, operand1_mode = None, operand2 = None, operand2_mode = None, operand3 = None,  operand3_mode = POSITION_MODE):
  11.         self.operand1 = operand1
  12.         self.operand2 = operand2
  13.         self.operand3 = operand3
  14.         self.operand1_mode = operand1_mode
  15.         self.operand2_mode = operand2_mode
  16.         self.operand3_mode = operand3_mode
  17.  
  18.     @abstractmethod
  19.     def operate(self, memory: List[int]) -> List[int]:
  20.         pass
  21.  
  22. class UnaryInstruction(InstructionBase):
  23.  
  24.     def __init__(self, operand1, operand1_mode = None):
  25.         super().__init__(operand1, operand1_mode)
  26.  
  27. class BinaryInstruction(InstructionBase):
  28.  
  29.     def __init__(self, operator, operand1, operand1_mode = None, operand2 = None, operand2_mode = None, operand3 = None,  operand3_mode = POSITION_MODE):
  30.         super().__init__(operand1, operand1_mode, operand2, operand2_mode, operand3, operand3_mode)
  31.         self.operator = operator
  32.  
  33.     def operate(self, memory: List[int]) -> List[int]:
  34.         if self.operand1_mode == POSITION_MODE:
  35.             a = memory[memory[self.operand1]]
  36.         else:
  37.             a = memory[self.operand1]
  38.        
  39.         if self.operand2_mode == POSITION_MODE:
  40.             b = memory[memory[self.operand2]]
  41.         else:
  42.             b = memory[self.operand2]
  43.        
  44.         value = self.operator(a, b)
  45.         if self.operand3_mode == POSITION_MODE:
  46.             memory[self.operand3] = value
  47.         else:
  48.             raise ValueError('Not implemented')
  49.        
  50.         return memory
  51.  
  52. class ReadInput(UnaryInstruction):
  53.  
  54.    
  55.     def __init__(self, operand1, input):
  56.         super().__init__(operand1)
  57.         self.input = input
  58.  
  59.     def operate(self, memory: List[int]) -> List[int]:
  60.         memory[self.operand1] = self.input
  61.         return memory
  62.  
  63. class PrintOutput(UnaryInstruction):
  64.  
  65.    
  66.     def __init__(self, operand1, operand1_mode):
  67.         super().__init__(operand1, operand1_mode)
  68.  
  69.     def operate(self, memory: List[int]) -> List[int]:
  70.         if self.operand1_mode == POSITION_MODE:
  71.             print(memory[self.operand1])
  72.         else:
  73.             print(memory[memory[self.operand1]])
  74.         return memory
  75.  
  76. class Add(BinaryInstruction):
  77.  
  78.     def __init__(self, operand1, operand1_mode = None, operand2 = None, operand2_mode = None, operand3 = None,  operand3_mode = POSITION_MODE):
  79.         operator = lambda x, y: x + y
  80.         super().__init__(operator, operand1, operand1_mode, operand2, operand2_mode, operand3, operand3_mode)
  81.  
  82.        
  83.  
  84. class Multiply(BinaryInstruction):
  85.  
  86.     def __init__(self, operand1, operand1_mode = None, operand2 = None, operand2_mode = None, operand3 = None,  operand3_mode = POSITION_MODE):
  87.         operator = lambda x, y: x * y
  88.         super().__init__(operator, operand1, operand1_mode, operand2, operand2_mode, operand3, operand3_mode)
  89.  
  90.  
  91. def instructionFactory(memory: List[int], input) -> List[InstructionBase]:
  92.     i = 0
  93.     instructions = []
  94.     while memory[i] != 99:
  95.         if memory[i] == 3:
  96.             instructions.append(ReadInput(memory[i + 1], input))
  97.             i = i + 2
  98.         else:
  99.             opcode = str(memory[i]).rjust(5, '0')
  100.             mode1 = int(opcode[-3])
  101.             mode2 = int(opcode[-4])
  102.             mode3 = int(opcode[0])
  103.             if opcode[-2:] == '01':
  104.                 instructions.append(Add(i + 1, mode1, i + 2, mode2, i + 3, mode3))
  105.                 i = i + 4
  106.             elif opcode[-2:] == '02':
  107.                 instructions.append(Multiply(i + 1, mode1, i + 2, mode2, i + 3, mode3))
  108.                 i = i + 4
  109.             else:
  110.                 instructions.append(PrintOutput(i + 1, mode1))
  111.                 i = i + 2
  112.  
  113.  
  114.  
  115.     return instructions
  116.  
  117.  
  118. def runProgram(memory: List[int]):
  119.     instructions = instructionFactory(memory, 1)
  120.     print(instructions)
  121.  
  122.     for instruction in instructions:
  123.         memory = instruction.operate(memory)
  124.  
  125.  
  126. if __name__ == "__main__":
  127.     with open('day5.txt', 'r') as f:
  128.         memory = [int(num) for num in f.readline().split(',')]
  129.         runProgram(memory)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement