common.zig (1167B)
1 const std = @import("std"); 2 3 pub const Setup = struct { 4 pub const Part = enum { pt1, pt2 }; 5 a: std.mem.Allocator, 6 input: []const u8, 7 part: Part, 8 9 pub fn get(a: std.mem.Allocator) !Setup { 10 var res: Setup = undefined; 11 res.a = a; 12 var p = std.process.args(); 13 var file: []const u8 = ""; 14 if (!p.skip()) return error.NoProgramName; 15 if (p.next()) |pp| { 16 if (std.mem.eql(u8, pp, "pt1")) { 17 res.part = .pt1; 18 } else if (std.mem.eql(u8, pp, "pt2")) { 19 res.part = .pt2; 20 } else { 21 return error.BadPartSpecified; 22 } 23 } else { 24 return error.NoPartSpecified; 25 } 26 if (p.next()) |pp| { 27 file = pp; 28 } else { 29 return error.NoPartSpecified; 30 } 31 32 var f = try std.fs.cwd().openFile(file, .{}); 33 defer f.close(); 34 const c = try f.readToEndAlloc(res.a, std.math.maxInt(u32)); 35 res.input = c; 36 return res; 37 } 38 pub fn deinit(self: *Setup) void { 39 self.a.free(self.input); // For some reason this is freezing 40 } 41 };