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 // Memory
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
pub const alloc = gpa.allocator(); pub const alloc = gpa.allocator();
pub const test_alloc = std.testing.allocator;
// Logging // Logging
pub const log = std.log.default; pub const log = std.log.default;
// Functions // Functions
pub fn tokenizeAllInput(allocator: Allocator, outputType: type, day: u8) !TokenIterator { pub fn inputTo2x2(comptime T: type, allocator: Allocator, input: []const T, delimiter: T) !std.ArrayList([]const T) {
var fileName: [16]u8 = undefined; var out = std.ArrayList([]const T).init(allocator);
const inputFile = try std.fmt.bufPrint(&fileName, "day{d}.txt", .{day}); errdefer out.deinit();
var dir = try std.fs.cwd().openDir("../data", .{}); var items = std.mem.tokenizeScalar(u8, input, delimiter);
defer dir.close(); while (items.next()) |item| {
try out.append(item);
}
const file = try dir.openFile(inputFile, .{}); return out;
defer file.close();
const stat = try file.stat();
const input = try file.reader().readAllAlloc(allocator, stat.size);
return inputToArrayList(outputType, allocator, input);
} }
pub fn readAllInput(day: u8, allocator: std.mem.Allocator) ![]const u8 { pub fn readAllInput(day: u8, allocator: std.mem.Allocator) ![]const u8 {