diff options
Diffstat (limited to 'day1.zig')
-rw-r--r-- | day1.zig | 47 |
1 files changed, 5 insertions, 42 deletions
@@ -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; |