const std = @import("std");

const Time_Distance = struct {
    time: u64,
    distance: u64,
};

fn day6_part1() !void {
    var input_file = try std.fs.cwd().openFile("inputs/day6.txt", .{});
    defer input_file.close();

    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    var allocator = arena.allocator();

    var time_distances = std.ArrayList(Time_Distance).init(allocator);
    _ = time_distances;

    var file_contents = try input_file.readToEndAlloc(allocator, 100 * 1024);

    var line_split = std.mem.splitScalar(u8, file_contents, '\n');

    var time_array = std.ArrayList(u64).init(allocator);
    var distance_array = std.ArrayList(u64).init(allocator);

    var times_line = line_split.next().?;
    var times_numbers_section = times_line[std.mem.indexOfScalar(u8, times_line, ':').? + 1 ..];

    var distance_line = line_split.next().?;
    var distance_numbers_section = distance_line[std.mem.indexOfScalar(u8, distance_line, ':').? + 1 ..];

    var times_tokens = std.mem.tokenizeScalar(u8, times_numbers_section, ' ');
    var time_concat = try std.fmt.parseInt(u64, try std.mem.replaceOwned(u8, allocator, times_numbers_section, " ", ""), 10);
    _ = time_concat;
    while (times_tokens.next()) |token| {
        try time_array.append(try std.fmt.parseInt(u64, token, 10));
    }

    var distance_tokens = std.mem.tokenizeScalar(u8, distance_numbers_section, ' ');
    var distance_concat = try std.fmt.parseInt(u64, try std.mem.replaceOwned(u8, allocator, distance_numbers_section, " ", ""), 10);
    _ = distance_concat;
    while (distance_tokens.next()) |token| {
        try distance_array.append(try std.fmt.parseInt(u64, token, 10));
    }

    var result: u64 = 0;
    for (time_array.items, distance_array.items) |time, distance| {
        var count: u64 = 0;
        for (0..time) |t| {
            if (t * (time - t) > distance) count += 1;
        }
        result = @max(result, 1) * count;
    }

    std.log.info("Part 1: {d}", .{result});
}