From a27a6df8b4f4700284d2b867642f3b53aedec06c Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Thu, 19 Dec 2024 18:42:09 -0500 Subject: [PATCH] 2024: util.zig: add `inputTo2x2()` --- 2024/src/util.zig | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/2024/src/util.zig b/2024/src/util.zig index c8288b3..52deaeb 100644 --- a/2024/src/util.zig +++ b/2024/src/util.zig @@ -5,24 +5,22 @@ const TokenIterator = std.mem.TokenIterator; // Memory var gpa = std.heap.GeneralPurposeAllocator(.{}){}; pub const alloc = gpa.allocator(); +pub const test_alloc = std.testing.allocator; // Logging pub const log = std.log.default; // Functions -pub fn tokenizeAllInput(allocator: Allocator, outputType: type, day: u8) !TokenIterator { - var fileName: [16]u8 = undefined; - const inputFile = try std.fmt.bufPrint(&fileName, "day{d}.txt", .{day}); +pub fn inputTo2x2(comptime T: type, allocator: Allocator, input: []const T, delimiter: T) !std.ArrayList([]const T) { + var out = std.ArrayList([]const T).init(allocator); + errdefer out.deinit(); - var dir = try std.fs.cwd().openDir("../data", .{}); - defer dir.close(); + var items = std.mem.tokenizeScalar(u8, input, delimiter); + while (items.next()) |item| { + try out.append(item); + } - const file = try dir.openFile(inputFile, .{}); - defer file.close(); - - const stat = try file.stat(); - const input = try file.reader().readAllAlloc(allocator, stat.size); - return inputToArrayList(outputType, allocator, input); + return out; } pub fn readAllInput(day: u8, allocator: std.mem.Allocator) ![]const u8 {