diff options
author | Christian Segundo | 2022-12-03 11:51:40 +0100 |
---|---|---|
committer | Christian Segundo | 2022-12-03 11:51:40 +0100 |
commit | 78fa87cd43cd18f2f9ec4a04c45bcb8036143fd8 (patch) | |
tree | 56af86be97e50d4f8c6926e27eea84d15ba2f2e6 /day-3/main.zig | |
parent | 6990eb44ae6111ac18667e416dc4dc2883c50f25 (diff) | |
download | advent-of-zig-2022-78fa87cd43cd18f2f9ec4a04c45bcb8036143fd8.tar.gz |
add day 3
Diffstat (limited to 'day-3/main.zig')
-rw-r--r-- | day-3/main.zig | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/day-3/main.zig b/day-3/main.zig new file mode 100644 index 0000000..4406a79 --- /dev/null +++ b/day-3/main.zig @@ -0,0 +1,38 @@ +const std = @import("std"); +const slurp = @import("util/file.zig").slurp; +const dupl_values = @import("util/mem.zig").dupl_values; + +var gpa = std.heap.GeneralPurposeAllocator(.{}){}; +const allocator = gpa.allocator(); + +pub fn main() !void { + const file_buffer = try slurp(allocator, "./input"); + defer allocator.free(file_buffer); + + var iter = std.mem.split(u8, file_buffer, "\n"); + + var count: u16 = 0; + while (iter.next()) |line| { + const duplicates = try dupl_values( + u8, + allocator, + &[_][]const u8{ + line[0 .. line.len / 2], + line[line.len / 2 ..], + }, + ); + defer allocator.free(duplicates); + + for (duplicates) |char| { + count += char_to_priority(char); + } + } + + std.debug.print("{d}\n", .{count}); +} + +fn char_to_priority(char: u8) u8 { + if (char >= 65 and char <= 90) + return char - 38; + return char - 96; +} |