-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.py
More file actions
95 lines (79 loc) · 2.66 KB
/
Copy pathcommon.py
File metadata and controls
95 lines (79 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import re
OPERATION_PATTERN = re.compile(
r"^ *(x[0-9]+|[!]?[a-g]) = (x[0-9]+) (.) (x[0-9]+) .*= ([01]+)"
)
def negate(bits):
return "".join("1" if b == "0" else "0" for b in bits)
TARGETS = [
[negate("1011011111100011"), "~a"],
[negate("1111100111100100"), "~b"],
[negate("1101111111110100"), "~c"],
[negate("1011011011011110"), "~d"],
[negate("1010001010111111"), "~e"],
[negate("1000111111110011"), "~f"],
["0011111011111111", "g"],
]
def read_chain_for_evaluation(chain_steps):
chain = []
for line in chain_steps:
match = OPERATION_PATTERN.match(line)
if match:
name, operand1, operator, operand2, bits = match.groups()
chain.append(
{
"name": name,
"operand1": operand1,
"operator": operator,
"operand2": operand2,
"bits": bits,
}
)
return chain
def evaluate_chain(chain, inputs):
result = {}
for operation in chain:
# print(inputs, operation)
input1 = inputs[operation["operand1"]]
input2 = inputs[operation["operand2"]]
operator = operation["operator"]
name = operation["name"]
bits = operation["bits"]
if operator == "|":
inputs[name] = input1 | input2
elif operator == "&":
inputs[name] = input1 & input2
elif operator == "^":
inputs[name] = input1 ^ input2
elif operator == "<":
inputs[name] = 1 if input1 < input2 else 0
elif operator == ">":
inputs[name] = 1 if input1 > input2 else 0
for [target_bits, target] in TARGETS:
if target_bits.startswith(bits):
result[target] = inputs[name]
break
return result
def ascii_digits(chain_steps):
chain = read_chain_for_evaluation(chain_steps)
digits = []
for k in range(16):
inputs = {
"x1": 1 if k & 8 else 0,
"x2": 1 if k & 4 else 0,
"x3": 1 if k & 2 else 0,
"x4": 1 if k & 1 else 0,
}
result = evaluate_chain(chain, inputs)
s1 = " {a} ".format(a="_" if not result["~a"] else " ")
s2 = "{f}{g}{b}".format(
b="|" if not result["~b"] else " ",
f="|" if not result["~f"] else " ",
g="_" if result["g"] else " ",
)
s3 = "{e}{d}{c}".format(
c="|" if not result["~c"] else " ",
e="|" if not result["~e"] else " ",
d="_" if not result["~d"] else " ",
)
digits.append((k, s1, s2, s3, result))
return digits