mirror of
https://codeberg.org/andyscott/advent-of-code.git
synced 2024-12-21 17:13:10 -05:00
2024: util.zig: add inputTo2x2()
This commit is contained in:
parent
cfdad32c84
commit
a27a6df8b4
1 changed files with 9 additions and 11 deletions
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue