Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -96,7 +96,11 @@ should generate the output:
: <code>11111111111111111111</code>
==Ruleset 5==
A simple Turing machine, implementing a three-state busy beaver. The tape consists of 0s and 1s, the states are A, B, C and H (for Halt), and the head position is indicated by writing the state letter before the character where the head is. All parts of the initial tape the machine operates on have to be given in the input.
A simple [http://en.wikipedia.org/wiki/Turing_machine Turing machine],
implementing a three-state [http://en.wikipedia.org/wiki/Busy_beaver busy beaver].
The tape consists of 0s and 1s, the states are A, B, C and H (for Halt), and the head position is indicated by writing the state letter before the character where the head is.
All parts of the initial tape the machine operates on have to be given in the input.
Besides demonstrating that the Markov algorithm is Turing-complete, it also made me catch a bug in the C++ implementation which wasn't caught by the first four rulesets.
<pre>

View file

@ -0,0 +1,63 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Markov {
public static void main(String[] args) throws IOException {
List<String[]> rules = readRules("markov_rules.txt");
List<String> tests = readTests("markov_tests.txt");
Pattern pattern = Pattern.compile("^([^#]*?)\\s+->\\s+(\\.?)(.*)");
for (int i = 0; i < tests.size(); i++) {
String origTest = tests.get(i);
List<String[]> captures = new ArrayList<>();
for (String rule : rules.get(i)) {
Matcher m = pattern.matcher(rule);
if (m.find()) {
String[] groups = new String[m.groupCount()];
for (int j = 0; j < groups.length; j++)
groups[j] = m.group(j + 1);
captures.add(groups);
}
}
String test = origTest;
String copy = test;
for (int j = 0; j < captures.size(); j++) {
String[] c = captures.get(j);
test = test.replace(c[0], c[2]);
if (c[1].equals("."))
break;
if (!test.equals(copy)) {
j = -1; // redo loop
copy = test;
}
}
System.out.printf("%s\n%s\n\n", origTest, test);
}
}
private static List<String> readTests(String path)
throws IOException {
return Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8);
}
private static List<String[]> readRules(String path)
throws IOException {
String ls = System.lineSeparator();
String lines = new String(Files.readAllBytes(Paths.get(path)), "UTF-8");
List<String[]> rules = new ArrayList<>();
for (String line : lines.split(ls + ls))
rules.add(line.split(ls));
return rules;
}
}