Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. import sys
  2.  
  3.  
  4. class Gate:
  5. def __init__(self, index: int, operation: str, input_gates):
  6. self._index = index
  7. self._operation = operation
  8. self._input_gates = input_gates
  9.  
  10. def __str__(self):
  11. if self._operation is None:
  12. return 'OUTPUT ' + str(self._index) + ' ' + ' '.join([str(item.get_id()) for item in self._input_gates])
  13. else:
  14. return 'GATE ' + str(self._index) + ' ' + self._operation + ' ' \
  15. + ' '.join([str(item.get_id()) for item in self._input_gates])
  16.  
  17. def get_id(self):
  18. return self._index
  19.  
  20.  
  21. class InputGate:
  22. def __init__(self, index):
  23. self._index = index
  24.  
  25. def get_id(self):
  26. return self._index
  27.  
  28.  
  29. def create_input(n):
  30. a = []
  31. b = []
  32. c = []
  33. for idx in range(n):
  34. a.append(InputGate(idx))
  35. b.append(InputGate(idx + n))
  36. c.append(InputGate(idx + 2 * n))
  37. return a, b, c
  38.  
  39.  
  40. def build_xor_median_block(start_index, a, b, c):
  41. gate_1 = Gate(start_index, 'AND', [a, b])
  42. gate_2 = Gate(start_index + 1, 'AND', [a, c])
  43. gate_3 = Gate(start_index + 2, 'AND', [b, c])
  44. gate_4 = Gate(start_index + 3, 'OR', [a, b])
  45. gate_5 = Gate(start_index + 4, 'NOT', [gate_1])
  46. gate_6 = Gate(start_index + 5, 'AND', [gate_4, gate_5]) # XOR a, b
  47. gate_7 = Gate(start_index + 6, 'OR', [gate_1, gate_2])
  48. gate_8 = Gate(start_index + 7, 'OR', [gate_7, gate_3]) # Median(a,b,c)
  49. gate_9 = Gate(start_index + 8, 'OR', [gate_6, c])
  50. gate_10 = Gate(start_index + 9, 'AND', [gate_6, c])
  51. gate_11 = Gate(start_index + 10, 'NOT', [gate_10])
  52. gate_12 = Gate(start_index + 11, 'AND', [gate_11, gate_9]) # XOR_3(a,b,c)
  53. gates = [gate_1, gate_2, gate_3, gate_4, gate_5, gate_6, gate_7, gate_8, gate_9, gate_10, gate_11, gate_12]
  54. return gates, gate_12, gate_8, start_index + 12
  55.  
  56.  
  57. def build_first_block(start_index, a, b, c):
  58. gates, gate_12, gate_8, start_index = build_xor_median_block(start_index, a, b, c)
  59. gate_13 = Gate(start_index + 1, 'AND', [gates[0], gates[4]]) # gate_1 & not gate_1
  60. gates.append(gate_13)
  61. return gates, gate_12, gate_8, gate_13, start_index + 1
  62.  
  63.  
  64. def read_input():
  65. n = int(input())
  66. return n
  67.  
  68.  
  69. def main():
  70. n = read_input()
  71. a, b, c = create_input(n)
  72. x = []
  73. y = []
  74. all_gates = []
  75. zero = None
  76. index = 3 * n
  77. for idx in range(n):
  78. if idx == 0:
  79. gates, t0, t1, zero, index = build_first_block(index, a[idx], b[idx], c[idx])
  80. else:
  81. gates, t0, t1, index = build_xor_median_block(index, a[idx], b[idx], c[idx])
  82. if idx == 0:
  83. y.append(Gate(n + 1, None, [zero]))
  84. x.append(Gate(idx, None, [t0]))
  85. if idx == n - 1:
  86. x.append(Gate(idx + 1, None, [zero]))
  87. y.append(Gate(idx + 1 + n + 1, None, [t1]))
  88. for gate in gates:
  89. all_gates.append(gate)
  90. for gate in all_gates:
  91. sys.stdout.write(str(gate) + '\n')
  92. for gate in x:
  93. sys.stdout.write(str(gate) + '\n')
  94. for gate in y:
  95. sys.stdout.write(str(gate) + '\n')
  96.  
  97.  
  98. if __name__ == '__main__':
  99. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement