#import "Basic";
#import "File";
#import "String";
#import "Math";

solve_day6 :: (test: bool) {
    contents := read_entire_file("inputs/day6.txt");
    lines := split(contents, "\n");

    times_split := split(trim(slice(lines[0], 5, lines[0].count - 5), " "), " ");
    distances_split := split(trim(slice(lines[1], 9, lines[1].count - 9), " "), " ");

    times: [..]u64;    
    distances: [..]u64;

    for times_split {
        if it.count == 0  continue;
        array_add(*times, string_to_int(it, 10, u64));
    }

    for distances_split {
        if it.count == 0  continue;
        array_add(*distances, string_to_int(it, 10, u64));
    }

    part1 := 0;

    for i_dt: 0..times.count - 1 {
        distance := distances[i_dt];
        count := 0;
        for 0..times[i_dt] - 1 {
            if it * (times[i_dt] - it) > distance  count += 1;    
        }

        part1 = max(part1, 1) * count;
    }
    print("Part 1: %\n", part1);

    time_part2 := string_to_int(replace(slice(lines[0], 5, lines[0].count), " ", ""), 10, s64);
    distance_part2 := string_to_int(replace(slice(lines[1], 9, lines[1].count), " ", ""), 10, s64);

    not_working_left  := 0;
    not_working_right := 0;

    for 0..time_part2 {
        p2 := time_part2 - it;
        if it * (time_part2 - it) > distance_part2 {
            not_working_left = cast(s64)it - 1;
        }
        if p2 * (time_part2 - p2) > distance_part2 {
            not_working_right = time_part2 - cast(s64)p2;
        }

        if not_working_left > 0 && not_working_right > 0  break;
    }

    print("Part 2: %\n", time_part2 - not_working_left - not_working_right);
}