Read file
This commit is contained in:
parent
31c3dc97ca
commit
a8ee9f637e
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
inputs
|
||||||
|
zig-out
|
||||||
|
zig-cache
|
||||||
|
|
5
inputs_test/01
Normal file
5
inputs_test/01
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
1abc2
|
||||||
|
pqr3stu8vwx
|
||||||
|
a1b2c3d4e5f
|
||||||
|
treb7uchet
|
||||||
|
|
41
src/main.zig
41
src/main.zig
@ -1,19 +1,44 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
|
const print = std.debug.print;
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
||||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
||||||
|
|
||||||
// stdout is for the actual output of your application, for example if you
|
try day01part1(file_name(false, "01"));
|
||||||
// are implementing gzip, then only the compressed bytes should be sent to
|
try day01part1(file_name(true, "01"));
|
||||||
// stdout, not any debugging messages.
|
}
|
||||||
const stdout_file = std.io.getStdOut().writer();
|
|
||||||
var bw = std.io.bufferedWriter(stdout_file);
|
|
||||||
const stdout = bw.writer();
|
|
||||||
|
|
||||||
try stdout.print("Run `zig build test` to run the tests.\n", .{});
|
fn day01part1(filename: []const u8) !void {
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
defer _ = gpa.deinit();
|
||||||
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
try bw.flush(); // don't forget to flush!
|
const file = try std.fs.cwd().openFile(filename, .{});
|
||||||
|
defer file.close();
|
||||||
|
|
||||||
|
var buf_reader = std.io.bufferedReader(file.reader());
|
||||||
|
const reader = buf_reader.reader();
|
||||||
|
|
||||||
|
var line = std.ArrayList(u8).init(allocator);
|
||||||
|
defer line.deinit();
|
||||||
|
|
||||||
|
const writer = line.writer();
|
||||||
|
while (reader.streamUntilDelimiter(writer, '\n', null)) {
|
||||||
|
defer line.clearRetainingCapacity();
|
||||||
|
|
||||||
|
print(":D {s}\n", .{line.items});
|
||||||
|
} else |err| switch (err) {
|
||||||
|
error.EndOfStream => {},
|
||||||
|
else => return err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates an input file name at comptime
|
||||||
|
fn file_name(comptime is_test: bool, comptime day: []const u8) []const u8 {
|
||||||
|
const input_suffix = if (is_test) "_test/" else "/";
|
||||||
|
return "inputs" ++ input_suffix ++ day;
|
||||||
}
|
}
|
||||||
|
|
||||||
test "simple test" {
|
test "simple test" {
|
||||||
|
Loading…
Reference in New Issue
Block a user