aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common.zig46
-rw-r--r--day1.zig47
-rw-r--r--day2.zig8
-rw-r--r--day2_sample.in0
4 files changed, 59 insertions, 42 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 :)
+ }
+};
diff --git a/day1.zig b/day1.zig
index ed6d7e9..5258b4e 100644
--- a/day1.zig
+++ b/day1.zig
@@ -1,5 +1,6 @@
const std = @import("std");
const digits = "0123456789";
+const Setup = @import("common.zig").Setup;
const words = [_][]const u8{
"0",
@@ -47,51 +48,13 @@ const w2d = std.comptime_string_map.ComptimeStringMap(u8, .{
.{ "nine", 9 },
});
-const Input = struct {
- const Part = enum { pt1, pt2 };
- file: []const u8,
- part: Part,
-};
-
-pub fn getInput() !Input {
- var p = std.process.args();
- var in: Input = .{
- .file = "",
- .part = .pt1,
- };
- if (!p.skip()) return error.NoProgramName;
- if (p.next()) |pp| {
- if (std.mem.eql(u8, pp, "pt1")) {
- in.part = .pt1;
- } else if (std.mem.eql(u8, pp, "pt2")) {
- in.part = .pt2;
- } else {
- return error.BadPartSpecified;
- }
- } else {
- return error.NoPartSpecified;
- }
- if (p.next()) |pp| {
- in.file = pp;
- } else {
- return error.NoPartSpecified;
- }
- return in;
-}
-
pub fn main() !void {
- var gpa = std.heap.GeneralPurposeAllocator(.{}){};
- defer _ = gpa.deinit();
- const a = gpa.allocator();
- const in = try getInput();
- var f = try std.fs.cwd().openFile(in.file, .{});
- defer f.close();
- var c = try f.readToEndAlloc(a, std.math.maxInt(u32));
- defer a.free(c);
- var l = std.mem.splitScalar(u8, c, '\n');
+ var s = try Setup.get();
+ defer s.deinit();
+ var l = std.mem.splitScalar(u8, s.input, '\n');
var t: u64 = 0;
while (l.next()) |n| {
- switch (in.part) {
+ switch (s.part) {
.pt1 => {
const ix1 = std.mem.indexOfAny(u8, n, digits) orelse return error.NoDigits;
const ix2 = std.mem.lastIndexOfAny(u8, n, digits) orelse return error.NoDigits;
diff --git a/day2.zig b/day2.zig
new file mode 100644
index 0000000..d4f8f85
--- /dev/null
+++ b/day2.zig
@@ -0,0 +1,8 @@
+const std = @import("std");
+const Setup = @import("common.zig").Setup;
+
+pub fn main() !void {
+ var setup = try Setup.get();
+ defer setup.deinit();
+
+} \ No newline at end of file
diff --git a/day2_sample.in b/day2_sample.in
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/day2_sample.in