#P1886. Gates of Logic
Gates of Logic
Problem Description
The Department of Computer Science and Engineering runs courses dealing not only with algorithms but also with computer hardware. One such introductory course explains basic principles of integrated circuits (“chips”), binary logic, boolean algebra, etc. As you may know, the very basic units of logical circuits are called gates. A gate is an element performing one simple logical operation. It can be connected to other gates using lines.

Logical circuits may be drawn as pictures with the gates represented as squares with inputs on the left and outputs on the right. In each square, there is a symbol that determines the gate type: Number 1 denotes an OR gate (its outputs are 0 if and only if there is no input with the value of 1), & is an AND gate (outputs are 1 if and only if there is no 0 input), and = is a XOR gate (outputs are 1 if and only if there is an odd number inputs that have the value of 1).
Your task is to scan such a “picture” and compute values of all named circuit outputs. The lines may split and join again but you may assume that each “value consumer” (input port of a gate or a named output) will be connected to exactly one “value source” (output port of a gate or an input value). There will be no feedback loops, i.e., there exists no cycle that would lead through the same gate twice.
Input
The input contains several pictures. Each picture consists of at least one and at most 200 rows composed of the following characters:
The rectangle size will always be at least 3 characters in both directions, which means there is at least one character inside. All inner characters are empty (spaces), with exactly one exception. That single non-empty character denotes the gate type (note that it may have different meaning than outside the gate area) and will be a digit "one" ("1"), ampersand ("&"), or an equal sign ("=").
Each picture will be terminated by a row consisting solely of asterisk ("*") characters (at least one). The last picture will be followed by two such rows. No row in the input will be longer than 200 characters.
Output
For each picture, print the values of all named outputs, sorted alphabetically. Each output row should contain three characters: output name (one uppercase letter), equals sign, and a binary value (zero or one). Print one empty line after each test case.
0=+
|
| #######
+=# #
# & #o=--+
1=------=# # |
# # |
####### +--=### ###
| #=#=#1#o==X
1=-----------x--=# # ###
1=---x--=###
+------------=Y
***********************************
1=A
***
*
X=0
Y=1
A=1