const std = @import("std");

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

const intType = u32;

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

    const input = @embedFile(path);
    var commands = std.mem.tokenize(u8, input, "\n");

    const Position = struct {
        forward: intType = 0,
        depth: intType = 0,
    };
    var pos: Position = .{};

    while (commands.next()) |c| {
        // example: commands = forward 8
        var comm_parts = std.mem.tokenize(u8, c, " ");

        const direction = comm_parts.next().?;
        const value = try std.fmt.parseUnsigned(intType, comm_parts.next().?, 10);

        if (std.mem.eql(u8, direction, "forward")) {
            pos.forward += value;
        }
        if (std.mem.eql(u8, direction, "down")) {
            pos.depth += value;
        }
        if (std.mem.eql(u8, direction, "up")) {
            pos.depth -= value;
        }
    }

    return pos.forward * pos.depth;
}

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

    try std.testing.expectEqual(ret, @as(u32, 1893605));

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

test "day02a" {
    try std.testing.expectEqual(@as(u32, 1893605), try first(std.testing.allocator));
}