From 9a5201154c28923c402d93e3ddee4858c10d8017 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Sat, 2 Dec 2023 14:02:11 +0000 Subject: Pull out some common parts for day2 --- day1.zig | 47 +++++------------------------------------------ 1 file changed, 5 insertions(+), 42 deletions(-) (limited to 'day1.zig') 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; -- cgit v1.2.3-ZIG