const std = @import("std"); const lineLength = 4; // maximum line length (digits) const decimalSize = u13; // every num in input is < 2^13 const maxItems = 2000; // number of items in input file const path = "data/day01/input.txt"; pub fn second(allocator: ?std.mem.Allocator) anyerror!decimalSize { _ = allocator; const values = try parseValues(); var inc: decimalSize = 0; for (values) |_, idx| { if (idx >= 3) { // as idx-2 and idx-1 are in a and b too, this can be simplified // const a = @intCast(u15, values[idx - 3]) + values[idx - 2] + values[idx - 1]; // const b = @intCast(u15, values[idx - 2]) + values[idx - 1] + values[idx]; const a = values[idx - 3]; const b = values[idx]; if (a < b) { inc += 1; } } } return inc; } fn parseValues() anyerror![maxItems]decimalSize { const file = @embedFile(path); var lines = std.mem.tokenize(u8, file, "\n"); var values: [maxItems]decimalSize = undefined; var i: decimalSize = 0; while (lines.next()) |v| : (i += 1) { values[i] = try std.fmt.parseUnsigned(decimalSize, v, 10); } return values; } pub fn main() anyerror!void { var timer = try std.time.Timer.start(); const ret = try second(null); const s = timer.read() / 1000; try std.testing.expectEqual(ret, @as(decimalSize, 1429)); std.debug.print("Day 1b result: {d} \t\ttime: {d}us\n", .{ ret, s }); } test "day01b" { try std.testing.expectEqual(@as(decimalSize, 1429), try second(std.testing.allocator)); }