Last active
May 30, 2020 15:33
-
-
Save EZLiang/a00743d92695cff51317782e27d54877 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Use like: python circuitgen.py /12/3 | |
or whatever generations rule you want to use | |
""" | |
import sys | |
transitions = { | |
"0": "00000000", | |
"1c": "01000000", "1e": "10000000", | |
"2c": "01010000", "2e": "10100000", "2k": "10010000", "2a": "11000000", "2i": "10001000", "2n": "01000100", | |
"3c": "01010100", "3e": "10101000", "3k": "10100100", "3a": "11100000", "3i": "01110000", "3n": "11010000", "3y": "10010100", "3q": "11000100", "3j": "11000010", "3r": "11001000", | |
"4c": "01010101", "4e": "10101010", "4k": "11010010", "4a": "11110000", "4i": "11011000", "4n": "11000101", "4y": "11010100", "4q": "11100100", "4j": "11010100", "4r": "11101000", "4t": "11001001", "4w": "11011000", "4z": "11001100", | |
"5c": "10101011", "5e": "11010101", "5k": "11010110", "5a": "11110001", "5i": "11111000", "5n": "11101001", "5y": "11011010", "5q": "11101100", "5j": "11110100", "5r": "11011100", | |
"6c": "11111010", "6e": "11110101", "6k": "10111101", "6a": "11111100", "6i": "11011101", "6n": "11101110", | |
"7c": "11111110", "7e": "01111111", | |
"8": "11111111" | |
} | |
def mix(n, s1, s2, c1, c2): | |
i = (n - s2) / (s1 - s2) | |
return int((c1 * i) + (c2 * (1 - i))) | |
def parse(string): | |
s = string.replace("1", "|1").replace("2", "|2").replace("3", "|3").replace("4", "|4").replace("5", "|5").replace("5", "|5").replace("7", "|7").replace("8", "|8").split("|") | |
st = [] | |
for i in s: | |
if not (i == ""): | |
n = i[0] | |
t = list(i) | |
del t[0] | |
if t == []: | |
for j in transitions.keys(): | |
if j[0] == str(n): | |
st.append(f"{j}") | |
elif t[0] == "-": | |
del t[0] | |
ts = [] | |
for j in transitions.keys(): | |
if j[0] == str(n): | |
ts.append(j[1]) | |
for j in ts: | |
if not (i in t): | |
st.append(f"{n}{j}") | |
else: | |
for j in t: | |
st.append(f"{n}{j}") | |
return st | |
def generate(string): | |
b = string.split("/")[1] | |
s = string.split("/")[0] | |
c = int(string.split("/")[2]) | |
result = "" | |
result += f"""@RULE Wire_B{b.replace("-", "_")}_S{s.replace("-", "_")}_C{c}\n\n************************************\n* RULE AUTOGENERATED BY CIRCUITGEN *\n************************************\n\n@TABLE\nn_states: {c + 1}\nneighborhood: Moore\nsymmetries: rotate4reflect\n\n""" | |
var_dead = "0,1" | |
for i in range(3, c + 1): | |
var_dead += "," | |
var_dead += str(i) | |
for i in range(8): | |
result += "var dead.{} = {}\n".format(i, "{" + var_dead + "}") | |
for i in range(8): | |
result += "var any.{} = {}\n".format(i, "{2," + var_dead + "}") | |
result += "\n" | |
bt = parse(b) | |
st = parse(s) | |
if not (bt == []): | |
for i in bt: | |
j = 0 | |
t = list(transitions[i].replace("1", "2")) | |
for i in range(8): | |
if t[i] == "0": | |
t[i] = f"dead.{j}" | |
j += 1 | |
result += "1,{},2\n".format(",".join(t)) | |
if not (st == []): | |
for i in st: | |
j = 0 | |
t = list(transitions[i].replace("1", "2")) | |
for i in range(8): | |
if t[i] == "0": | |
t[i] = f"dead.{j}" | |
j += 1 | |
result += "2,{},2\n".format(",".join(t)) | |
result += "2,any.0,any.1,any.2,any.3,any.4,any.5,any.6,any.7,3\n" | |
result += f"{c},any.0,any.1,any.2,any.3,any.4,any.5,any.6,any.7,1\n" | |
for i in range(3, c): | |
result += f"{i},any.0,any.1,any.2,any.3,any.4,any.5,any.6,any.7,{i + 1}\n" | |
result += f"\n@COLORS\n0 48 48 48\n" | |
result += "1 0 128 255\n" | |
for i in range(2, c + 1): | |
result += f"{i} {mix(i, c + 1, 2, 0, 255)} {mix(i, c + 1, 2, 128, 255)} {255}\n" | |
return result | |
def main(): | |
x = generate(sys.argv[1]) | |
print(x) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment