Compare commits

...

2 Commits

@ -9,8 +9,6 @@ const Symbol = enum(u2) {
};
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"});
// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
@ -24,61 +22,46 @@ pub fn main() !void {
var bag = std.ArrayList([]Symbol).init(std.heap.page_allocator);
defer bag.deinit(); // try commenting this out and see if zig detects the memory leak!
var initial: [2]Symbol = .{Symbol.M, Symbol.I};
//var initial: [2]Symbol = .{Symbol.M, Symbol.I};
var initial: [2]Symbol = .{Symbol.M, Symbol.U};
try bag.append(&initial);
var count: u64 = 0;
while(bag.items.len > count){
std.debug.print("curr/total {d}/{d}, \n", .{count, bag.items.len});
var currentBatch = bag.items[count..];
count = bag.items.len;
var currentBatch = bag.items;
std.debug.print("testing strings: {any}\n", .{currentBatch});
//TODO: skip to [count] items
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(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)); }
if(finish(string)){
break; //Finished!
}
}
std.debug.print("Testing: {any}\n", .{bag});
std.debug.print("bag contains: {any}\n", .{bag.items});
std.debug.print("curr/total {d}/{d}, \n", .{count, bag.items.len});
count = bag.items.len;
currentBatch = bag.items;
std.debug.print("testing strings: {any}\n", .{currentBatch});
// if(rule1Test(string)){ try bag.append(try rule1(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)); }
if(rule1Test(string)){ try addToBag(bag,try rule1(string));}
if(rule2Test(string)){ try addToBag(bag,try rule2(string));}
if(rule3Test(string)){ try addToBag(bag,try rule3(string));}
if(rule4Test(string)){ try addToBag(bag,try rule4(string));}
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(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)); }
//TODO: Moet ik nog iets doen met het pad naar het anwoord?
if(finish(string)){
break; //Finished!
return; //Finished!
}
}
std.debug.print("bag contains: {any}\n", .{bag.items});
return;
}
@ -88,27 +71,31 @@ 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{
std.debug.print("Does {any} exist in {any}\n", .{new, list.items});
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", .{});
// Ends on I? Add U.
// Allocate space for new array
var arr = try std.heap.page_allocator.alloc(Symbol, input.len + 1);
//copy input into the allocated array
// Assignment doesn't work, that also changes the length of the destination.
//NOTE: Assignment doesn't work, that also changes the length of the destination.
mem.copy(Symbol, arr, input[0..]);
arr[arr.len-1] = Symbol.U;
std.debug.print("Rule1 result: {any}\n", .{arr});
return arr;
}
@ -117,19 +104,7 @@ 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..];
// arr[input.len] = input[1..];
// mem.copy([]Symbol, arr)
const arr = mem.concat(std.heap.page_allocator, Symbol, &[_][]Symbol{input[0..], input[1..]});
std.debug.print("Rule2 result: {any}\n", .{arr});
return arr;
}
@ -154,7 +129,6 @@ pub fn rule3(input: []Symbol) ![]Symbol{
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];
@ -203,7 +177,6 @@ pub fn rule4(input: []Symbol) ![]Symbol{
pub fn finish(input: []Symbol) bool{
const pattern: [2]Symbol = .{Symbol.M, Symbol.U};
return mem.eql(Symbol, input, &pattern);
}

Loading…
Cancel
Save