aboutsummaryrefslogtreecommitdiff
path: root/2021/01/second.zig
blob: eb6993ec046c155fa096502e64418d999f0577ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const std = @import("std");

const example = @embedFile("example");
const input = @embedFile("input");

pub fn main() anyerror!void {
    try std.testing.expectEqual(solve(example), 5);
    const result = try solve(input);
    try std.io.getStdOut().writer().print("{}\n", .{result});
}

fn solve(puzzle: []const u8) !u64 {
    var it = std.mem.tokenize(u8, puzzle, " \n");
    var tot: u64 = 0;
    var prev = [_]?u64{null} ** 3;
    while (it.next()) |value| {
        const n = try std.fmt.parseInt(u64, value, 10);
        if (prev[0]) |p| {
            if (p < n) {
                tot += 1;
            }
        }
        var i: usize = 0;
        while (i < prev.len - 1) : (i += 1) {
            prev[i] = prev[i + 1];
        }
        prev[2] = n;
    }
    return tot;
}