|
|
|
|
@ -2,7 +2,7 @@ const std = @import("std");
|
|
|
|
|
const mem = std.mem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const Symbol = enum {
|
|
|
|
|
const Symbol = enum(u2) {
|
|
|
|
|
M,
|
|
|
|
|
U,
|
|
|
|
|
I
|
|
|
|
|
@ -32,11 +32,15 @@ pub fn main() !void {
|
|
|
|
|
std.debug.print("curr/total {d}/{d}, \n", .{count, bag.items.len});
|
|
|
|
|
|
|
|
|
|
count = bag.items.len;
|
|
|
|
|
for(bag.items)|string|{
|
|
|
|
|
var currentBatch = bag.items;
|
|
|
|
|
std.debug.print("testing strings: {any}\n", .{currentBatch});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(currentBatch)|string|{
|
|
|
|
|
std.debug.print("Testing: {any}\n", .{string});
|
|
|
|
|
|
|
|
|
|
//if(rule1Test(string)){ try bag.append(try rule1(string)); }
|
|
|
|
|
if(rule1Test(string)){ try addToBag(bag,string);}
|
|
|
|
|
if(rule1Test(string)){ try bag.append(try rule1(string)); }
|
|
|
|
|
//if(rule1Test(string)){ try addToBag(bag,string);}
|
|
|
|
|
if(rule2Test(string)){ try bag.append(try rule2(string)); }
|
|
|
|
|
if(rule3Test(string)){ try bag.append(try rule3(string)); }
|
|
|
|
|
if(rule4Test(string)){ try bag.append(try rule4(string)); }
|
|
|
|
|
@ -44,7 +48,13 @@ pub fn main() !void {
|
|
|
|
|
if(finish(string)){
|
|
|
|
|
break; //Finished!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std.debug.print("bag contains: {any}\n", .{bag.items});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -54,23 +64,25 @@ pub fn main() !void {
|
|
|
|
|
try bw.flush(); // don't forget to flush!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn addToBag(list: std.ArrayList([]Symbol), new: []Symbol) !void{
|
|
|
|
|
if(mem.containsAtLeast([]Symbol, list.items, 1, new)){
|
|
|
|
|
try list.append(new);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// pub fn addToBag(list: std.ArrayList([]Symbol), new: []Symbol) !void{
|
|
|
|
|
// if(mem.containsAtLeast([]Symbol, list.items, 1, new)){
|
|
|
|
|
// try list.append(new);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
pub fn rule1Test(input: []Symbol ) bool{
|
|
|
|
|
return (input[input.len-1] == Symbol.I);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
pub fn rule1(input: []Symbol) ![]Symbol{
|
|
|
|
|
std.debug.print("Rule1\n", .{});
|
|
|
|
|
|
|
|
|
|
var arr = try std.heap.page_allocator.alloc(Symbol, input.len + 1);
|
|
|
|
|
arr = input[0..];
|
|
|
|
|
arr[arr.len-1] = Symbol.U;
|
|
|
|
|
|
|
|
|
|
std.debug.print("Rule1 result: {any}\n", .{arr});
|
|
|
|
|
return arr;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn rule2Test(input: []Symbol) bool {
|
|
|
|
|
@ -79,6 +91,8 @@ pub fn rule2Test(input: []Symbol) bool {
|
|
|
|
|
pub fn rule2(input: []Symbol) ![]Symbol{
|
|
|
|
|
//M_ => M__
|
|
|
|
|
|
|
|
|
|
std.debug.print("Rule2\n", .{});
|
|
|
|
|
|
|
|
|
|
// var arr = try std.heap.page_allocator.alloc(Symbol, input.len * 2);
|
|
|
|
|
|
|
|
|
|
// arr = input[0..];
|
|
|
|
|
@ -86,7 +100,10 @@ pub fn rule2(input: []Symbol) ![]Symbol{
|
|
|
|
|
|
|
|
|
|
// mem.copy([]Symbol, arr)
|
|
|
|
|
|
|
|
|
|
return mem.concat(std.heap.page_allocator, Symbol, &[_][]Symbol{input[0..], input[1..]});
|
|
|
|
|
const arr = mem.concat(std.heap.page_allocator, Symbol, &[_][]Symbol{input[0..], input[1..]});
|
|
|
|
|
std.debug.print("Rule2 result: {any}\n", .{arr});
|
|
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn rule3Test(input: []Symbol) bool {
|
|
|
|
|
@ -104,6 +121,8 @@ pub fn rule3Test(input: []Symbol) bool {
|
|
|
|
|
}
|
|
|
|
|
pub fn rule3(input: []Symbol) ![]Symbol{
|
|
|
|
|
// III => U
|
|
|
|
|
std.debug.print("Rule3\n", .{});
|
|
|
|
|
|
|
|
|
|
const pattern: [3]Symbol = .{Symbol.I, Symbol.I, Symbol.I};
|
|
|
|
|
var single = try std.heap.page_allocator.alloc(Symbol, 1);
|
|
|
|
|
single[0] = Symbol.U;
|
|
|
|
|
@ -137,6 +156,8 @@ pub fn rule4Test(input: []Symbol) bool {
|
|
|
|
|
}
|
|
|
|
|
pub fn rule4(input: []Symbol) ![]Symbol{
|
|
|
|
|
// UU => -
|
|
|
|
|
std.debug.print("Rule4\n", .{});
|
|
|
|
|
|
|
|
|
|
const pattern: [2]Symbol = .{Symbol.U, Symbol.U};
|
|
|
|
|
|
|
|
|
|
for(input)|symbol, i|{
|
|
|
|
|
|