Skip to content

Commit

Permalink
Read input into a Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Jan 10, 2024
1 parent 1023918 commit 8600615
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions day16/src/day16.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,37 @@ const std = @import("std");
const allocator = std.heap.page_allocator;
const stdout = std.io.getStdOut().writer();

const String = []u8;
const Matrix = std.ArrayList(String);

pub fn main() !u8 {
var args = try std.process.argsAlloc(allocator);
if (args.len <= 1) {
try stdout.print("Usage: {s} <path to input>\n", .{args[0]});
return 1;
}

var buf: [1024]u8 = undefined;
var matrix = Matrix.init(allocator);
defer matrix.deinit();

var buffer: [1024]u8 = undefined;
var file = try std.fs.cwd().openFile(args[1], .{});
var bufReader = std.io.bufferedReader(file.reader());
var reader = bufReader.reader();
defer file.close();

while (try reader.readUntilDelimiterOrEof(&buf, '\n')) |line| {
while (try reader.readUntilDelimiterOrEof(&buffer, '\n')) |bufLine| {
const line = try allocator.dupeZ(u8, bufLine);
try matrix.append(line);
}

defer {
for (matrix.items) |line| {
allocator.free(line);
}
}

for (matrix.items) |line| {
try stdout.print("{s}\n", .{line});
}

Expand Down

0 comments on commit 8600615

Please sign in to comment.