diff options
author | Martin Ashby <martin@ashbysoft.com> | 2023-12-02 14:02:11 +0000 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2023-12-02 14:02:11 +0000 |
commit | 9a5201154c28923c402d93e3ddee4858c10d8017 (patch) | |
tree | 1be2126e22afa231ebb06307a672128c1c70b294 /common.zig | |
parent | b580fd512a7659585a8a749d97cf4763b8343ccf (diff) | |
download | aoc2023-9a5201154c28923c402d93e3ddee4858c10d8017.tar.gz aoc2023-9a5201154c28923c402d93e3ddee4858c10d8017.tar.bz2 aoc2023-9a5201154c28923c402d93e3ddee4858c10d8017.tar.xz aoc2023-9a5201154c28923c402d93e3ddee4858c10d8017.zip |
Pull out some common parts for day2
Diffstat (limited to 'common.zig')
-rw-r--r-- | common.zig | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/common.zig b/common.zig new file mode 100644 index 0000000..f724940 --- /dev/null +++ b/common.zig @@ -0,0 +1,46 @@ +const std = @import("std"); + +pub const Setup = struct { + pub const Part = enum { pt1, pt2 }; + + gpa: std.heap.GeneralPurposeAllocator(.{}), + a: std.mem.Allocator, + input: []const u8, + part: Part, + + pub fn get() !Setup { + var res: Setup = undefined; + res.gpa = std.heap.GeneralPurposeAllocator(.{}){}; + res.a = res.gpa.allocator(); + 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(); + var c = try f.readToEndAlloc(res.a, std.math.maxInt(u32)); + res.input = c; + return res; + } + pub fn deinit(self: *Setup) void { + _ = self; + //self.a.free(self.input); // For some reason this is freezing + //_ = self.gpa.deinit(); // and if we don't free, then gpa.deinit whinges that we leaked memory :) + } +}; |