Execute a Markov algorithm: Difference between revisions
Content deleted Content added
Shorter lines in D entry, removed casts, formatting, some comments and blank lines added |
|||
Line 1,006: | Line 1,006: | ||
void main() { |
void main() { |
||
string[][] rules = readText("markov_rules.txt").splitLines(). |
|||
split(""); |
|||
string[] tests = readText("markov_tests.txt").splitLines(); |
|||
⚫ | |||
assert(tests.length == rules.length); |
|||
// Faster to compile run-time regex |
|||
//auto regex = regex(r"^([^#]*?)\s+->\s+(\.?)(.*)"); |
|||
// Compile-time regex creation |
|||
// DMD 2.059 uses about 130 MB RAM to compile this |
|||
⚫ | |||
foreach (i, test; tests) { |
foreach (i, test; tests) { |
||
string[][] capt; |
string[][] capt; |
||
foreach (line; rules[i]) { |
foreach (line; rules[i]) { |
||
auto m = match( |
auto m = line.match(regex); |
||
if (!m.empty) |
if (!m.empty) |
||
capt ~= m.captures.array()[1 .. $]; |
|||
⚫ | |||
} |
|||
⚫ | |||
auto copy = test; |
|||
foreach (c; capt) { |
foreach (c; capt) { |
||
test = test.replace(c[0], c[2]); |
test = test.replace(c[0], c[2]); |
||
if (c[1] == ".") |
if (c[1] == ".") |
||
break; |
|||
if (test != copy) |
|||
goto REDO; |
|||
} |
} |
||
writeln(test); |
writeln(test); |
||
} |
} |