Skip to content

Instantly share code, notes, and snippets.

@clarle
Created March 21, 2012 02:59
Show Gist options
  • Save clarle/2143943 to your computer and use it in GitHub Desktop.
Save clarle/2143943 to your computer and use it in GitHub Desktop.
Simple Circuit Solver
oparray = [ \
lambda x, y: False, # FALSE
lambda x, y: x and y, # AND
lambda x, y: x and (not y), # X AND NOT Y
lambda x, y: x, # X
lambda x, y: (not x) and y, # NOT X AND Y
lambda x, y: y, # Y,
lambda x, y: x != y, # XOR
lambda x, y: x or y, # OR
lambda x, y: not (x or y), # NOR
lambda x, y: (x and y) or ((not x) and (not y)), # XNOR
lambda x, y: not y, # NOT Y
lambda x, y: x or (not y), # X OR NOT Y
lambda x, y: not x, # NOT X
lambda x, y: (not x) or y, # NOT X OR Y
lambda x, y: not (x and y), # NAND
lambda x, y: True, # TRUE
]
boolhash = [ \
"FALSE",
"AND",
"X AND NOT Y",
"X",
"NOT X AND Y",
"Y",
"XOR",
"OR",
"NOR",
"XNOR",
"NOT Y",
"X OR NOT Y",
"NOT X",
"NOT X OR Y",
"NAND",
"TRUE"
]
def xor_network(x, y, f1, f2):
return f2((x and y), f1(x, y))
def check_network(f1, f2):
if xor_network(0, 0, f1, f2) != 0:
return False
if xor_network(1, 1, f1, f2) != 0:
return False
if xor_network(1, 0, f1, f2) != 1:
return False
if xor_network(0, 1, f1, f2) != 1:
return False
# else
return True
for i in xrange(16):
for j in xrange(16):
if check_network(oparray[i], oparray[j]):
print "O4: " + boolhash[i] + ", " + "O5: " + boolhash[j]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment