#Unexpected line breaks using debug print

1 messages · Page 1 of 1 (latest)

tropic basin
#

This is the first exercise in the AOC

The console prints the later part of the formatted string plus garbage??

 -> 9984zbjbznsqtj
 -> 999fhnmvqsoneninezbrzcqkz4ftv
1eightcrcjcbdthreebscfpvznqfrj6 -> 1000

I'm expecting it to print

eightbqfhnmvqsoneninezbrzcqkz4ftv -> 999
1eightcrcjcbdthreebscfpvznqfrj6 -> 1000

pub fn main() !void {
    const allocator = std.heap.page_allocator;
    const max_size = std.math.maxInt(usize);

    const data = try std.fs.cwd().readFileAlloc(allocator, "./input/01.txt", max_size);
    defer allocator.free(data);

    var iter = std.mem.splitScalar(u8, data, '\n');
    var i: u32 = 0;
    while (iter.next()) |line| {
        i += 1;

        std.debug.print("{s} -> {d}\n", .{ line, i });
        //std.debug.print("{s}\n", .{line}); // prints as expected
        //std.debug.print("{}\n", .{i}); // prints as expected
    }
}
void phoenix
#

if you are on windows try changing your while loop to this:

while (iter.next()) |line_untrim| {
  const line = std.mem.trim(u8, line_untrim, “\r”);
tropic basin
#

Doh, that worked. Thank you!