

fn print(msg: string) #[builtin(Print)] {}
fn println(msg: string) { print(msg + "\n") }

fn int_to_string(number: int) -> string #[builtin(IntToString)] {}

fn string_push_char(str: string, value: int) -> string #[builtin(StringPushChar)] {}
fn string_length(str: string) -> int #[builtin(StringLength)] {}
fn string_to_int(str: string) -> int #[builtin(StringToInt)] {}

fn array_new_string() -> [string] #[builtin(ArrayNew)] {}
fn array_new_int() -> [int] #[builtin(ArrayNew)] {}
fn array_push_string(array: [string], value: string) #[builtin(ArrayPush)] {}
fn array_push_int(array: [int], value: int) #[builtin(ArrayPush)] {}
fn array_length_string(array: [string]) -> int #[builtin(ArrayLength)] {}
fn array_length_int(array: [int]) -> int #[builtin(ArrayLength)] {}

fn char(ch: string) -> int {
    ch[0]
}

fn abs(number: int) -> int {
    let result = number;
    if number < 0 {
        result = number - (number * 2);
    }
    result
}

fn split(str: string, seperator: int) -> [string] {
    let result: [string] = array_new_string();

    let i = 0;
    let current_str = "";
    loop {
        if i >= string_length(str) {
            break;
        }
        let char = str[i];
        if char == seperator {
            array_push_string(result, current_str);
            current_str = "";
        } else {
            current_str = string_push_char(current_str, char);
        }
        i = i + 1;
    }
    array_push_string(result, current_str);
    result
}

fn slice(str: string, from: int, to: int) -> string {
    let result = "";
    let i = from;
    loop {
        if i >= string_length(str) {
            break;
        }
        if i >= to {
            break;
        }
        result = string_push_char(result, str[i]);
        i = i + 1;
    }
    result
}

// fn triangle_sort(array: [int]) -> [int] {
//     let result: [int] = array_new_int(); 
//     let i = 0;
//     loop {
//         if i >= array_length_int(array) {
//             break;
//         }
//         let j = i;
//         let current_lowest_int = array[0];
//         loop {
//             if j >= array_length_int(array) {
//                 break;
//             }
//             let current_int = array[j]; 
//             if current_int < current_lowest_int {
//                 current_lowest_int = current_int;
//             }
//             j = j + 1;
//         }
//         array_push_int(result, current_lowest_int);
//         i = i + 1;
//     }
//     result
// }

fn array_clone(array: [int]) -> [int] {
    let len = array_length_int(array);
    let result = array_new_int();
    let i = 0;
    loop {
        if i >= len { break; }
        result[i] = array[i];
        i = 1 + 1;
    }
    result
}

fn array_sort_mut(array: [int]) {
    let len = array_length_int(array);
    let i = 0;
    loop {
        if i >= len { break; }
        let j = i + 1;
        loop {
            if j >= len { break; }
            if array[j] < array[i] {
                let tmp = array[j];
                array[j] = array[i];
                array[i] = tmp;
            }
            j = j + 1;
        }
        i = 1 + 1;
    }
}

fn array_to_sorted(array: [int]) -> [int] {
    let cloned = array_clone(array);
    array_sort_mut(array);
    cloned
}

fn location_ids() -> string {
"49744   57964
20738   85861
20319   65072
79568   74248
78194   83454
48701   94102
69552   26808
62781   67392
85323   47428
99344   72568
27523   97243
48039   36600
91532   31571
21306   31571
52409   10805
33901   31571
80772   38756
13849   54584
72294   28326
86065   65553
93987   72533
81640   39741
25701   89912
98611   57082
80949   94974
84717   61876
31599   57082
87119   65871
56659   22897"
}

fn main() {
    let location_ids = split(location_ids(), char("\n"));
    let i = 0;
    let left_ids: [int] = array_new_int();
    let right_ids: [int] = array_new_int(); 
    loop {
        if i >= array_length_string(location_ids) {
            break;
        }
        array_push_int(left_ids, string_to_int(slice(location_ids[i], 0, 5)));
        array_push_int(right_ids, string_to_int(slice(location_ids[i], 8, 13)));
        i = i + 1;
    }

    let sorted_left_ids: [int] = array_to_sorted(left_ids);
    // let sorted_right_ids: [int] = array_to_sorted(right_ids);
    // i = 0;
    // let sum = 0;
    // loop {
    //     if i >= array_length_int(left_ids) {
    //         break;
    //     }
    //     let difference = abs(sorted_left_ids[i] - sorted_right_ids[i]);
    //     let sum = sum + difference;
    //     i = i + 1;
    // }
    // println(int_to_string(sum))
}
