-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualizer
74 lines (63 loc) · 2.05 KB
/
Visualizer
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
import java.util.Arrays;
public class Beaver {
private Integer[] tape;
private String[] rules;
private int index;
private int rule;
public Beaver(int len, String[] rules) {
tape = new Integer[len * 2];
for (int i = 0; i < len * 2; i++) tape[i] = 0;
this.rules = rules;
index = len;
rule = 0;
p(rules[0]);
}
private boolean eval() {
String current = rules[rule + (rules.length / 2) * tape[index]];
if (current.equals("H"))
return false;
tape[index] = (int) current.charAt(0) - 48;
index += current.charAt(1) == 'R' ? 1 : -1;
rule = current.charAt(2) - 65;
p(current);
return !(index == -1 || index == tape.length * 2);
}
private void p(String current) {
String ret = "";
int ind = 0;
for (int i : tape) {
if (ind == index)
ret += "\u001B[33m";
else if (i == 0)
ret += "\u001B[31m";
else
ret += "\u001B[32m";
ret += i + "\u001B[0m";
ind++;
}
ret += " ";
ret += current;
System.out.println(ret);
}
public void run() {
int count = 0;
while (index < tape.length) {
count++;
if (!eval())
break;
}
p("");
System.out.println(count + " Iterations");
}
// Put a length and ruleset where it says, uncomment the line, and run
//
public static void main(String[] args) {
// Beaver t = new Beaver(INTEGER LENGTH, new String[] {RULES});
t.run();
}
}
// Example Rules: Shamelessly stolen from wikipedia https://en.wikipedia.org/wiki/Busy_beaver#Visualizations
// 2 rules -- 3 wide {"1RB", "1LA", "1LB", "H"}
// 3 rules -- 5 wide {"1RB", "0RC", "1LC", "H", "1RB", "1LA"}
// 4 Rules -- 12 wide {"1RB", "1LA", "H", "1RD", "1LB", "0LC", "1LD", "0RA"}
// Skelet 1 (2542/∞ iterations) -- 81 wide {"1LC", "H", "1RD", "1LA", "0LB", "1LE", "1LD", "0LD", "1RE", "0RC"}