const std = @import("std");

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

const intType = u32;

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

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

    const Postition = struct {
        forward: intType = 0,
        depth: intType = 0,
        aim: intType = 0,
    };

    var pos: Postition = .{};

    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;
            pos.depth += value * pos.aim;
        }
        if (std.mem.eql(u8, direction, "down")) {
            pos.aim += value;
        }
        if (std.mem.eql(u8, direction, "up")) {
            pos.aim -= value;
        }
    }

    return pos.forward * pos.depth;
}

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

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

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

test "day02b" {
    try std.testing.expectEqual(@as(u32, 2120734350), try second(std.testing.allocator));
}