aboutsummaryrefslogtreecommitdiff
path: root/common.zig
blob: 9eb1c6e8ab94cad67756c1e2a79ea7f694d9508a (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
31
32
33
34
35
36
37
38
39
40
41
const std = @import("std");

pub const Setup = struct {
    pub const Part = enum { pt1, pt2 };
    a: std.mem.Allocator,
    input: []const u8,
    part: Part,

    pub fn get(a: std.mem.Allocator) !Setup {
        var res: Setup = undefined;
        res.a = a;
        var p = std.process.args();
        var file: []const u8 = "";
        if (!p.skip()) return error.NoProgramName;
        if (p.next()) |pp| {
            if (std.mem.eql(u8, pp, "pt1")) {
                res.part = .pt1;
            } else if (std.mem.eql(u8, pp, "pt2")) {
                res.part = .pt2;
            } else {
                return error.BadPartSpecified;
            }
        } else {
            return error.NoPartSpecified;
        }
        if (p.next()) |pp| {
            file = pp;
        } else {
            return error.NoPartSpecified;
        }

        var f = try std.fs.cwd().openFile(file, .{});
        defer f.close();
        const c = try f.readToEndAlloc(res.a, std.math.maxInt(u32));
        res.input = c;
        return res;
    }
    pub fn deinit(self: *Setup) void {
        self.a.free(self.input);  // For some reason this is freezing
    }
};