From 4f5e5310f38bd676981cf1d6f7346ff83210d006 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Wed, 4 Dec 2024 17:43:21 -0500 Subject: [PATCH] 2024: util.zig: add `inputToArrayList()` --- 2024/util.zig | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/2024/util.zig b/2024/util.zig index d67c2f8..e4e9d03 100644 --- a/2024/util.zig +++ b/2024/util.zig @@ -9,3 +9,13 @@ pub fn readAllInput(day: u8, allocator: std.mem.Allocator) ![]const u8 { const stat = try file.stat(); return try file.reader().readAllAlloc(allocator, stat.size); } + +pub fn inputToArrayList(comptime T: type, allocator: std.mem.Allocator, input: []const u8) !std.ArrayList(T) { + var out = std.ArrayList(T).init(allocator); + errdefer out.deinit(); + var items = std.mem.tokenizeScalar(u8, input, ' '); + while (items.next()) |item| { + try out.append(try std.fmt.parseInt(T, item, 10)); + } + return out; +}