summaryrefslogtreecommitdiff
path: root/day_03.zig
diff options
context:
space:
mode:
authorChristian Segundo2022-12-05 17:00:08 +0100
committerChristian Segundo2022-12-05 22:16:11 +0100
commit061a5bae272f45db6dcde99746922735f9769d25 (patch)
treee4dd9b6d903930c5c1b76286b4a08beea6f59162 /day_03.zig
parent8817203517907ef4248bde7474e6fb566515d6a7 (diff)
downloadadvent-of-zig-2022-061a5bae272f45db6dcde99746922735f9769d25.tar.gz
add day 5
Diffstat (limited to 'day_03.zig')
-rw-r--r--day_03.zig63
1 files changed, 63 insertions, 0 deletions
diff --git a/day_03.zig b/day_03.zig
new file mode 100644
index 0000000..b33c5cf
--- /dev/null
+++ b/day_03.zig
@@ -0,0 +1,63 @@
+const std = @import("std");
+const Result = @import("util/aoc.zig").Result;
+
+pub fn puzzle_1(input: []const u8) Result {
+ var iter = std.mem.split(u8, input, "\n");
+
+ var count: u16 = 0;
+ while (iter.next()) |line| {
+ var p1: u64 = 0;
+ var p2: u64 = 0;
+
+ // A note to whoever reads this in the future
+ // A to Z -> 65 to 90
+ // a to z -> 97 to 122
+ //
+ // u64 is ok
+ // 90 - 65 + 122 - 97 = 50
+
+ for (line[0 .. line.len / 2]) |_, i| {
+ p1 |= @as(u64, 1) << @intCast(u6, line[i] - 65); // 65 == 'A'
+ p2 |= @as(u64, 1) << @intCast(u6, line[i + line.len / 2] - 65);
+ }
+
+ // Example
+ //
+ // p1: 0000000101100010001000110000000000011000000011000010000100
+ // p2: 1000001000000000000110000000000000000001011001100001100000
+ //
+ // &: 0000000000000000000000000000000000000000000001000000000000 = 46 0s
+ //
+ // 65 + 63 - 46 = 82 = R
+
+ count += char_to_priority(@intCast(u8, 65 + 63) - @clz(p1 & p2));
+ }
+
+ return .{ .int = count };
+}
+
+pub fn puzzle_2(input: []const u8) Result {
+ var iter = std.mem.split(u8, input, "\n");
+
+ var count: u16 = 0;
+ mainLoop: while (true) {
+ var parts = std.mem.zeroes([3]u64);
+
+ for (parts) |*p| {
+ const line = iter.next() orelse break :mainLoop;
+ for (line) |_, i| {
+ p.* |= @as(u64, 1) << @intCast(u6, line[i] - 65);
+ }
+ }
+
+ count += char_to_priority(@intCast(u8, 65 + 63) - @clz(parts[0] & parts[1] & parts[2]));
+ }
+
+ return .{ .int = count };
+}
+
+fn char_to_priority(char: u8) u8 {
+ if (char >= 65 and char <= 90)
+ return char - 38;
+ return char - 96;
+}