Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- filename = 'input.txt'
- expressions = []
- truths = set()
- with open(filename) as file:
- lines = file.readlines()
- lines_count = len(lines)
- for i in range(lines_count):
- line = lines[i]
- if i < lines_count - 2:
- bools = re.findall('[A-Z]', line)
- operators = re.findall(r'\|\||&&', line)
- if len(bools) - len(operators) != 2:
- raise ValueError('Invalid expression on line #' + str(i + 1))
- expr = dict(bools=bools, operators=operators)
- expressions.append(expr)
- elif i == lines_count - 1:
- truths = set(re.findall('[A-Z]', line))
- while True:
- no_changes = True
- for expr in expressions:
- left = expr['bools'][0] in truths
- for i in range(len(expr['operators'])):
- op = expr['operators'][i]
- right = expr['bools'][i + 1] in truths
- if op == '&&':
- left = left and right
- else:
- left = left or right
- if left and expr['bools'][-1] not in truths:
- truths.add(expr['bools'][-1])
- no_changes = False
- break
- if no_changes: break
- print(sorted(truths))
Advertisement
Add Comment
Please, Sign In to add comment