|
|
|
|
@ -8,8 +8,6 @@ const Symbol = enum(u2) {
|
|
|
|
|
I
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn main() !void {
|
|
|
|
|
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
|
|
|
|
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
|
|
|
|
@ -33,10 +31,10 @@ pub fn main() !void {
|
|
|
|
|
while(list.items.len > count){
|
|
|
|
|
count = list.items.len;
|
|
|
|
|
for(list.items)|string|{
|
|
|
|
|
if(rule1Test(string)){ try list.append(rule1(string)); }
|
|
|
|
|
if(rule2Test(string)){ try list.append(rule2(string)); }
|
|
|
|
|
if(rule3Test(string)){ try list.append(rule3(string)); }
|
|
|
|
|
if(rule4Test(string)){ try list.append(rule4(string)); }
|
|
|
|
|
if(rule1Test(string)){ try list.append(try rule1(string)); }
|
|
|
|
|
if(rule2Test(string)){ try list.append(try rule2(string)); }
|
|
|
|
|
if(rule3Test(string)){ try list.append(try rule3(string)); }
|
|
|
|
|
if(rule4Test(string)){ try list.append(try rule4(string)); }
|
|
|
|
|
|
|
|
|
|
if(finish(string)){
|
|
|
|
|
break; //Finished!
|
|
|
|
|
@ -54,21 +52,29 @@ pub fn main() !void {
|
|
|
|
|
pub fn rule1Test(input: []Symbol ) bool{
|
|
|
|
|
return (input[input.len] == Symbol.I);
|
|
|
|
|
}
|
|
|
|
|
pub fn rule1(input: []Symbol) []Symbol{
|
|
|
|
|
const lastEl:Symbol = input[input.len];
|
|
|
|
|
const u: [1]Symbol = .{Symbol.U};
|
|
|
|
|
pub fn rule1(input: []Symbol) ![]Symbol{
|
|
|
|
|
var arr = try std.heap.page_allocator.alloc(Symbol, input.len + 1);
|
|
|
|
|
arr = input[0..];
|
|
|
|
|
arr[arr.len] = Symbol.U;
|
|
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
|
|
|
|
|
|
if(lastEl == Symbol.I){
|
|
|
|
|
return input ++ u;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn rule2Test(input: []Symbol) bool {
|
|
|
|
|
return input[0] == Symbol.M;
|
|
|
|
|
}
|
|
|
|
|
pub fn rule2(input: []Symbol) []Symbol{
|
|
|
|
|
pub fn rule2(input: []Symbol) ![]Symbol{
|
|
|
|
|
//M_ => M__
|
|
|
|
|
return input ++ input[1..];
|
|
|
|
|
|
|
|
|
|
// var arr = try std.heap.page_allocator.alloc(Symbol, input.len * 2);
|
|
|
|
|
|
|
|
|
|
// arr = input[0..];
|
|
|
|
|
// arr[input.len] = input[1..];
|
|
|
|
|
|
|
|
|
|
// mem.copy([]Symbol, arr)
|
|
|
|
|
|
|
|
|
|
return mem.concat(std.heap.page_allocator, Symbol, &[_][]Symbol{input[0..], input[1..]});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn rule3Test(input: []Symbol) bool {
|
|
|
|
|
@ -84,21 +90,26 @@ pub fn rule3Test(input: []Symbol) bool {
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
pub fn rule3(input: []Symbol) []Symbol{
|
|
|
|
|
pub fn rule3(input: []Symbol) ![]Symbol{
|
|
|
|
|
// III => U
|
|
|
|
|
const pattern: [3]Symbol = .{Symbol.I, Symbol.I, Symbol.I};
|
|
|
|
|
const u: [1]Symbol = .{Symbol.U};
|
|
|
|
|
var single = try std.heap.page_allocator.alloc(Symbol, 1);
|
|
|
|
|
single[0] = Symbol.U;
|
|
|
|
|
|
|
|
|
|
//var i:u64 = 0;
|
|
|
|
|
for(input)|symbol, i|{
|
|
|
|
|
if(symbol == Symbol.I){
|
|
|
|
|
var streak = input[i..i+2];
|
|
|
|
|
if(mem.eql(Symbol, streak, &pattern)){
|
|
|
|
|
return input[0..i] ++ u ++ input[i+2..];
|
|
|
|
|
//return input[0..i] ++ u ++ input[i+2..];
|
|
|
|
|
|
|
|
|
|
return mem.concat(std.heap.page_allocator, Symbol, &[_][]Symbol{input[0..i], single,input[i..]});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return error.NOTFOUND;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn rule4Test(input: []Symbol) bool {
|
|
|
|
|
@ -112,7 +123,7 @@ pub fn rule4Test(input: []Symbol) bool {
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
pub fn rule4(input: []Symbol) []Symbol{
|
|
|
|
|
pub fn rule4(input: []Symbol) ![]Symbol{
|
|
|
|
|
// UU => -
|
|
|
|
|
const pattern: [2]Symbol = .{Symbol.U, Symbol.U};
|
|
|
|
|
|
|
|
|
|
@ -120,10 +131,14 @@ pub fn rule4(input: []Symbol) []Symbol{
|
|
|
|
|
if(symbol == Symbol.U){
|
|
|
|
|
var streak = input[i..i+2];
|
|
|
|
|
if(mem.eql(Symbol, streak, &pattern)){
|
|
|
|
|
return input[0..i] ++ input[i+1..];
|
|
|
|
|
//return input[0..i] ++ input[i+1..];
|
|
|
|
|
return mem.concat(std.heap.page_allocator, Symbol, &[_][]Symbol{input[0..i], input[i+2..]});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return error.NOTFOUND;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn finish(input: []Symbol) bool{
|
|
|
|
|
|