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 second(allocator: ?std.mem.Allocator) anyerror!usize { _ = allocator; return sum(try countFish(256)); } 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(usize, 1590327954513)); std.debug.print("Day 6b result: {d} \ttime: {d}us\n", .{ ret, s }); } test "day06b" { try std.testing.expectEqual(@as(usize, 1590327954513), try second(std.testing.allocator)); }