const std = @import("std");

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

const ages = 9;
const AgeType = u4;

fn parseInput() anyerror![ages]usize {
    const in = @embedFile(path);
    const input = std.mem.trimRight(u8, in, "\r\n");
    var ltrs = std.mem.tokenize(u8, input, ",");

    var ret = [_]usize{0} ** ages;

    while (ltrs.next()) |lantern_age| {
        const age = try std.fmt.parseUnsigned(AgeType, lantern_age, 10);
        ret[age] += 1;
    }

    return ret;
}

fn sum(ageArray: [ages]usize) usize {
    var s: usize = 0;
    for (ageArray) |value| {
        s += value;
    }
    return s;
}

fn countFish(days: u9) anyerror![ages]usize {
    var age = try parseInput();

    var i: @TypeOf(days) = 0;
    while (i < days) : (i += 1) {
        const new = age[0];
        age[0] = age[1];
        age[1] = age[2];
        age[2] = age[3];
        age[3] = age[4];
        age[4] = age[5];
        age[5] = age[6];
        age[6] = age[7] + new;
        age[7] = age[8];
        age[8] = new;
    }

    return age;
}

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

    return sum(try countFish(80));
}

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(usize, 350149));

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

test "day06a" {
    try std.testing.expectEqual(@as(usize, 350149), try first(std.testing.allocator));
}