4FDJNKVA3K4JLSUPNALFOVXU3OLTNSOWEPUTS2RQGKM5YXWM74YAC
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;
}
fn first() anyerror!usize {
return sum(try countFish(80));
}
fn second() anyerror!usize {
return sum(try countFish(256));
}
pub fn main() anyerror!void {
var timer = try std.time.Timer.start();
_ = try first();
const f = timer.lap() / 1000;
_ = try second();
const s = timer.lap() / 1000;
std.debug.print("Day 6 \t\tfirst: {d}µs \t\tsecond: {d}µs\n", .{ f, s });
}
test "first" {
try std.testing.expectEqual(@as(usize, 350149), try first());
}
test "second" {
try std.testing.expectEqual(@as(usize, 1590327954513), try second());
}
4,1,4,1,3,3,1,4,3,3,2,1,1,3,5,1,3,5,2,5,1,5,5,1,3,2,5,3,1,3,4,2,3,2,3,3,2,1,5,4,1,1,1,2,1,4,4,4,2,1,2,1,5,1,5,1,2,1,4,4,5,3,3,4,1,4,4,2,1,4,4,3,5,2,5,4,1,5,1,1,1,4,5,3,4,3,4,2,2,2,2,4,5,3,5,2,4,2,3,4,1,4,4,1,4,5,3,4,2,2,2,4,3,3,3,3,4,2,1,2,5,5,3,2,3,5,5,5,4,4,5,5,4,3,4,1,5,1,3,4,4,1,3,1,3,1,1,2,4,5,3,1,2,4,3,3,5,4,4,5,4,1,3,1,1,4,4,4,4,3,4,3,1,4,5,1,2,4,3,5,1,1,2,1,1,5,4,2,1,5,4,5,2,4,4,1,5,2,2,5,3,3,2,3,1,5,5,5,4,3,1,1,5,1,4,5,2,1,3,1,2,4,4,1,1,2,5,3,1,5,2,4,5,1,2,3,1,2,2,1,2,2,1,4,1,3,4,2,1,1,5,4,1,5,4,4,3,1,3,3,1,1,3,3,4,2,3,4,2,3,1,4,1,5,3,1,1,5,3,2,3,5,1,3,1,1,3,5,1,5,1,1,3,1,1,1,1,3,3,1