const std = @import("std");

const path = "data/day17/input.txt";

const RetType = u14;

const TargetValue = i9;
const TargetArea = struct {
    xmin: TargetValue,
    xmax: TargetValue,
    ymin: TargetValue,
    ymax: TargetValue,
};

fn parseInput() !TargetArea {
    const input = @embedFile(path);
    const in = std.mem.trimRight(u8, input, "\n");

    var ta: TargetArea = undefined;

    var coords = std.mem.tokenize(u8, in[13..], ", ");

    var xpart = coords.next().?[2..];
    var xcords = std.mem.tokenize(u8, xpart, "..");
    ta.xmin = try std.fmt.parseInt(TargetValue, xcords.next().?, 10);
    ta.xmax = try std.fmt.parseInt(TargetValue, xcords.next().?, 10);

    var ypart = coords.next().?[2..];
    var ycords = std.mem.tokenize(u8, ypart, "..");
    ta.ymin = try std.fmt.parseInt(TargetValue, ycords.next().?, 10);
    ta.ymax = try std.fmt.parseInt(TargetValue, ycords.next().?, 10);

    return ta;
}

pub fn first(allocator: ?std.mem.Allocator) !RetType {
    _ = allocator;

    const ta = try parseInput();

    // https://github.com/timvisee/advent-of-code-2021/issues/9
    var ret = @intCast(RetType, -ta.ymin);
    ret *= @intCast(RetType, -ta.ymin - 1);
    ret /= 2;

    return ret;
}

pub fn main() !void {
    var timer = try std.time.Timer.start();
    const ret = try first(null);
    const t = timer.lap() / 1000;

    try std.testing.expectEqual(@as(RetType, 5778), ret);

    std.debug.print("Day 17a result: {d} \t\ttime: {d}us\n", .{ ret, t });
}

test "day17a" {
    try std.testing.expectEqual(@as(RetType, 5778), try first(std.testing.allocator));
}