Skip to content

Instantly share code, notes, and snippets.

@jtsshieh
Created April 6, 2023 02:03
Show Gist options
  • Save jtsshieh/9653db4a5f9210d9d85a87e373c4823e to your computer and use it in GitHub Desktop.
Save jtsshieh/9653db4a5f9210d9d85a87e373c4823e to your computer and use it in GitHub Desktop.
import itertools;
numberOfLines = int(input())
statements = []
for i in range(0, numberOfLines):
statements.append(input())
letters = []
for statement in statements:
letter = statement.replace("and", "").replace("not", "").replace(" ", "")
if letter[0] not in letters:
letters.append(letter[0])
if letter[1] not in letters:
letters.append(letter[1])
table = list(itertools.product([False, True], repeat=len(letters)))
output = ""
for truthCombination in table:
if True not in truthCombination:
continue
hitCombination = True
declarations = {}
for i in range(len(letters)):
declarations[letters[i]] = truthCombination[i]
for statement in statements:
parts = statement.split(" and ")
result = False
if not parts[1].startswith("not"):
result = declarations[parts[0]] and declarations[parts[1]]
else:
if parts[0].startswith('not'):
result = not declarations[parts[0].replace("not ", "")] and not declarations[parts[1].replace("not ", "")]
else:
result = declarations[parts[0]] and not declarations[parts[1].replace("not ", "")]
if result == True:
hitCombination = False
break
if hitCombination:
for i in range(len(truthCombination)):
if truthCombination[i] == True:
output += letters[i]
break
if output == "":
print("IMPOSSIBLE")
else:
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment