diff --git a/day1-zig/input/day1_example2.txt b/day1-zig/input/day1_example2.txt new file mode 100644 index 0000000..bf8f9e1 --- /dev/null +++ b/day1-zig/input/day1_example2.txt @@ -0,0 +1,4 @@ +L1000 +L50 +R50 +R50 diff --git a/day1-zig/src/main.zig b/day1-zig/src/main.zig index 07c4c68..57098ba 100644 --- a/day1-zig/src/main.zig +++ b/day1-zig/src/main.zig @@ -3,37 +3,109 @@ //! is to delete this file and start with root.zig instead. pub fn main() !void { - // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); + const part1Solution = try part1("input/day1.txt"); + std.debug.print("Part1:{}\n", .{part1Solution}); - // stdout is for the actual output of your application, for example if you - // are implementing gzip, then only the compressed bytes should be sent to - // 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", .{}); - - try bw.flush(); // Don't forget to flush! + const part2Solution = try part2("input/day1.txt"); + std.debug.print("Part2:{}\n", .{part2Solution}); } -fn parse(input) +fn part1(path: []const u8) !i32 { + var file = try std.fs.cwd().openFile(path, .{}); + defer file.close(); -test "Part 1 sample" { - const input = try std.fs.cwd().readFile("day1_example.txt", .{}); - -} + var buf_reader = std.io.bufferedReader(file.reader()); + var in_stream = buf_reader.reader(); -test "fuzz example" { - const Context = struct { - fn testOne(context: @This(), input: []const u8) anyerror!void { - _ = context; - // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case! - try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input)); + var pos: i32 = 50; + var count: i32 = 0; + + var buf: [100]u8 = undefined; + while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| { + const shift = try std.fmt.parseInt(i32, line[1..], 10); + if (line[0] == 'L') { + pos -= shift; + while (pos < 0) { + pos += 100; + } + } else { + pos += shift; + pos = @mod(pos, 100); } - }; - try std.testing.fuzz(Context{}, Context.testOne, .{}); + if (pos == 0) { + count += 1; + } + } + return count; } +fn part2(path: []const u8) !i32 { + var file = try std.fs.cwd().openFile(path, .{}); + defer file.close(); + + var buf_reader = std.io.bufferedReader(file.reader()); + var in_stream = buf_reader.reader(); + + var pos: i32 = 50; + var count: i32 = 0; + + var buf: [100]u8 = undefined; + while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| { + var shift = try std.fmt.parseInt(i32, line[1..], 10); + if (shift > 100) { + count += @divFloor(shift, 100); + shift = @mod(shift, 100); + } + const startPos = pos; + if (line[0] == 'L') { + pos -= shift; + if (pos < 0) { + pos += 100; + if (startPos != 0) { + count += 1; + } + } + } else { + pos += shift; + if (pos > 100) { + count += 1; + pos = @mod(pos, 100); + } else if (pos == 100) { + pos = 0; + } + } + if (pos == 0) { + count += 1; + } + std.debug.print("pos: {}, count: {}\n", .{ pos, count }); + } + return count; +} + +test "Part 1 example" { + const res = try part1("input/day1_example.txt"); + try std.testing.expect(res == 3); +} + +test "Part 2 example" { + const res = try part2("input/day1_example.txt"); + try std.testing.expect(res == 6); +} + +test "Part 2 example 2" { + const res = try part2("input/day1_example2.txt"); + try std.testing.expect(res == 12); +} + +// test "fuzz example" { +// const Context = struct { +// fn testOne(context: @This(), input: []const u8) anyerror!void { +// _ = context; +// // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case! +// try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input)); +// } +// }; +// try std.testing.fuzz(Context{}, Context.testOne, .{}); +// } + const std = @import("std");