const std = @import("std");
var data : [4][1024]u32 = undefined;
var res:u32=0;
var res2:u32=0;

pub fn main() !void {
_ = try readFile();
std.debug.print("{},{}\n",.{data[0][0],data[1][0]});

    for(&data[0],0..) | *elema,indexa |
    {
        const slice = data[0][indexa+1..];
        for(slice) |*elemb|
        {
            if(elemb.* < elema.*)
            {
                const temp = elema.*;
                elema.* = elemb.*;
                elemb.* = temp;
            }
        }
    }
    for(&data[1],0..) | *elema,indexa |
    {
        const slice = data[1][indexa+1..];
        for(slice) |*elemb|
        {
            if(elemb.* < elema.*)
            {
                const temp = elema.*;
                elema.* = elemb.*;
                elemb.* = temp;
            }
        }
    }

    for(&data[0],&data[1],&data[2],&data[3]) | a, b, *c, *d |
    {
        d.* = 0;
        if(a < b)
        {
            c.* = b-a;
        }
        else if (a > b)
        {
            c.* = a-b;
        }
    }
    for(&data[0]) | a |
    {

        for(&data[1], &data[3]) | b, *c |
        {
        //std.debug.print("a:{} ,b:{}\n",.{a,b});
            if(a == b and a != 0)
            {
                c.* = a;
                std.debug.print("c:{}, a:{}\n",.{c.*,a});
            }
        }
    }
    for(&data[3]) | count |
    {
        res2 = res2 + count;
    }

    for(&data[2]) | a |
    {
        res = res + a;
    }
    std.debug.print("res: {}\n",.{res});
    std.debug.print("res2: {}\n",.{res2});
    std.debug.print("ex: {} {} {}",.{data[0][1],data[1][1],data[3][1]});    


}



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

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

    var buf: [1024]u8 = undefined;
    var index : u32 = 0;
    var indexa: u32 = 0;
    var indexb : 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)
                    {
                        //std.debug.print("entry:{s}\n", .{word});
                        if(@mod(index,2)==0)
                        {
                            data[0][indexa] = try std.fmt.parseInt(u32,word,10);
                            indexa +=1;
                        }
                        else
                        {
                            data[1][indexb] = try std.fmt.parseInt(u32,word,10);
                            indexb += 1;
                        }
                        index += 1;
                    }
                }
            }

        }
}

