Advent of Code 2020 solutions in Zig
const std = @import("std");

const PATH = "input/day01.txt";
const MAX_LINES = 200;

pub fn first(allocator: ?std.mem.Allocator) anyerror!usize {
    _ = allocator;

    const input = try parseInput();

    for (input) |item| {
        for (input) |elem| {
            if (item + elem == 2020) {
                return item * elem;
            }
        }
    }

    unreachable;
}

pub fn second(allocator: ?std.mem.Allocator) anyerror!usize {
    _ = allocator;

    const input = try parseInput();

    for (input) |item| {
        for (input) |elem| {
            for (input) |third| {
                if (item + elem + third == 2020) {
                    return item * elem * third;
                }
            }
        }
    }

    unreachable;
}

fn parseInput() ![]usize {
    const file = @embedFile(PATH);
    var lines = std.mem.tokenize(u8, file, "\n");

    var ret: [MAX_LINES]usize = undefined;

    var counter: usize = 0;
    while (lines.next()) |line| : (counter += 1) {
        ret[counter] = try std.fmt.parseUnsigned(usize, line, 0);
    }

    return &ret;
}

test "day01a" {
    try std.testing.expectEqual(@as(usize, 482811), try first(std.testing.allocator));
}

test "day01b" {
    try std.testing.expectEqual(@as(usize, 193171814), try second(std.testing.allocator));
}