2024: util.zig: add inputTo2x2()

This commit is contained in:
Andrew Scott 2024-12-19 18:42:09 -05:00
parent cfdad32c84
commit a27a6df8b4
Signed by: a
GPG key ID: 7CD5A5977E4931C1

View file

@ -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 {