-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautosearch.py
41 lines (30 loc) · 1.19 KB
/
autosearch.py
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
import cellular
import search
def automaton(rule: int, rows: int, character: chr):
"print automaton with rule and amount of rows"
# set to default view
current_row = 2 ** (rows - 1)
for i in range(rows):
out = search.search(current_row, character)
if out > -1:
print(out, i)
return
current_row = cellular.nextRow(rows * 2 + 1, current_row, rule)
def encode(rule: int, rows: int, character: chr):
"print automaton with rule and amount of rows"
# amount of cells in each row
# we make them intentionally wider so the outcome is not influenced by edges
cells_no = 4 * rows - 3
# the first row has 1 in the middle
current_row = 2 ** (2 * rows - 2)
for i in range(rows):
trimmed_row = (current_row >> (rows - 1)) % (2 ** (2 * rows - 1))
out = search.search(trimmed_row, character)
if out > -1:
print(character, rows - (out - 3), i)
return
current_row = cellular.nextRow(cells_no, current_row, rule)
def encodeString(rule: int, rows: int, text: str):
"encode a string with a cellular automaton"
for i in range(len(text)):
encode(rule, rows, text[i])