const std = @import("std");
const print = std.debug.print;
var data : [1024][20]i32 = undefined;
var data_len : [1024]u32 = undefined;
var res:u32=0;
var res2:u32=0;
const dir = enum {zero,up,down};
var data_size : u32 = 0;
var direction = dir.zero;
var safe : bool = true;
var safe_rep : u32 = 0;
var safe_no_dump : u32 = 0;
    //const e_data = data[0..data_size];
var temp : i32 = 0;
var legit : bool = false;
var instance_unsafe : u32 = 0;

pub fn main() !void {
    _ = try readFile();
    const e_data = data[0..data_size];
    const e_data_len = data_len[0..data_size];

    for(e_data,e_data_len) | row,len |
    {
        print("[main]len:{}\n",.{len});
        const row2 = row[0..len];
        if(checkLevel(row2) > 0)
        {
            safe_no_dump += 1;
            for(1..len) | i |
            {
                print("\x1B[0m",.{});
                const row1 = try rmNum(&row,len,i);
                print("rmRow: {d}\n",.{row1});
                print("remove level {}\n",.{i-1});
                if(checkLevel(row1)>0)
                {
                    print("still unsafe ",.{});
                }
                else
                {
                    print("now safe ",.{});
                    safe_rep += 1;
                    break;
                }
            }
        }
        else
        {
            safe_rep += 1;
        }
    }
print("Data_size: {}\n",.{data_size});
std.debug.print("safe reports: {}\nsafe rep without dump:{}\n",.{safe_rep,safe_no_dump});
}
fn rmNum(slice : [] const i32,len : u32,pos : usize) ![]i32
{
    var str = try std.heap.page_allocator.alloc(i32,len);

    print("len rm: {}, slicelen: {}\n",.{len,slice.len});

    for(slice,0..) | num, i |
    {
        if(i > len-1)
        {
            break;
        }

        if(num == 0) break;
        if(i < pos)
        {
            str[i] = num;
        }
        else
        {
            str[i-1] = num;
        }
    }
    const sl2 = str[0..len-1];
    return sl2;
}

fn checkLevel(slice : [] const i32) u8
{
            direction = dir.zero;
            legit = false;
            print("slice: {d}, len {}\n",.{slice,slice.len});

            for(slice,0..) | num,ii |
            {
                if(ii < slice.len-1 )
                {
                    if(num == slice[ii+1])
                    {
                        std.debug.print("\x1B[31mequal1\n",.{});
                        return 1;
                    }
                    if(direction == dir.zero and num > slice[ii+1])
                    {
                        direction = dir.down;
                    }
                    if(direction == dir.zero and num < slice[ii+1])
                    {
                        direction = dir.up;
                    }
                    if(direction == dir.down and num < slice[ii+1])
                    {
                        std.debug.print("\x1B[31mexit2\n",.{});
                        return 2;
                    }
                    if(direction == dir.up and num > slice[ii+1])
                    {
                        std.debug.print("\x1B[31mexit3\n direction : {}\n num :{} > {}, index: {}\n",.{direction,num,slice[ii+1],ii});
                        return 3;
                    }
                    //temp = @intCast(num);
                    if(@abs(num - slice[ii+1]) < 1 or @abs(num - slice[ii+1]) > 3)
                    {
                        std.debug.print("\x1B[31mexit4 num: {} slice[ii+1]: {}, full slice {d}, index: {}\n,",.{num,slice[ii+1],slice,ii});
                        return 4;
                    }

                }

            }
            print("\x1B[0m",.{});
            return 0;
}

fn readFile() !void
{
    const file = try std.fs.cwd().openFile("day2-1.txt",.{});
    defer file.close();

    var buf_reader = std.io.bufferedReader(file.reader());
    var in_stream = buf_reader.reader();

    var buf: [1024*5]u8 = undefined;
    var index : u32 = 0;
    var row : u32 = 0;

    while(try in_stream.readUntilDelimiterOrEof(&buf,'\n')) | line | 
        {
            var it = std.mem.split(u8,line," ");

            while (it.next()) |word| 
            {
                if(std.mem.eql(u8,word,"\n")==false)
                {
                    if(std.mem.eql(u8,word," ")==false)
                    {
                        data[row][index] = try std.fmt.parseInt(i32,word,10);
                        index += 1;
                    }
                }
                else
                {

                    data_len[row] = index;
                    row += 1;
                    index = 0;
                }
            }
            data_len[row] = index;
            index = 0;
            row += 1;

        }
        data_size = row;
}

