const std = @import("std");
const fmt = std.fmt;
const fs = std.fs;
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";
fn first() anyerror!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);
}
var inc: decimalSize = 0;
for (values) |v, idx| {
if (idx == 0) continue;
if (v > values[idx - 1]) {
inc += 1;
}
}
return inc;
}
fn second() anyerror!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 fmt.parseUnsigned(decimalSize, v, 10);
}
var inc: decimalSize = 0;
for (values) |_, idx| {
if (idx >= 3) {
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];
if (a < b) {
inc += 1;
}
}
}
return inc;
}
pub fn main() anyerror!void {
var timer = try std.time.Timer.start();
_ = try first();
const f = timer.lap() / 1000;
_ = try second();
const s = timer.read() / 1000;
std.debug.print("Day 1 \tfirst: {d}μs \tsecond: {d}μs\n", .{ f, s });
}
test "day01a" {
try std.testing.expectEqual(@as(decimalSize, 1400), try first());
}
test "day01b" {
try std.testing.expectEqual(@as(decimalSize, 1429), try second());
}