mirror of
https://codeberg.org/andyscott/advent-of-code.git
synced 2024-12-22 01:23:11 -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
|
// 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 {
|
||||||
|
|
Loading…
Reference in a new issue