about summary refs log tree commit diff
path: root/day01.zig
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2022-12-02 16:33:53 +0100
committerLeah Neukirchen <leah@vuxu.org>2022-12-02 16:34:48 +0100
commit3569846dad2bae95412762363ff9301f0540ab62 (patch)
treedecf8a3acda957acfb51198da576be834287ae27 /day01.zig
parent37c6618115dc8d028b0e62dc11149973d733ef7c (diff)
downloadadventofcode2022-3569846dad2bae95412762363ff9301f0540ab62.tar.gz
adventofcode2022-3569846dad2bae95412762363ff9301f0540ab62.tar.xz
adventofcode2022-3569846dad2bae95412762363ff9301f0540ab62.zip
day01
Diffstat (limited to 'day01.zig')
-rw-r--r--day01.zig33
1 files changed, 33 insertions, 0 deletions
diff --git a/day01.zig b/day01.zig
new file mode 100644
index 0000000..a12204c
--- /dev/null
+++ b/day01.zig
@@ -0,0 +1,33 @@
+const std = @import("std");
+const data = @embedFile("day01");
+
+var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
+const gpa = general_purpose_allocator.allocator();
+
+fn moreThan(context: void, a: i32, b: i32) std.math.Order {
+    _ = context;
+    return std.math.order(b, a);
+}
+
+pub fn main() !void {
+    var pq = std.PriorityQueue(i32, void, moreThan).init(gpa, {});
+
+    var lines = std.mem.split(u8, data, "\n");
+
+    var food: i32 = 0;
+    while (lines.next()) |line| {
+        var x: i32 = std.fmt.parseInt(i32, line, 10) catch {
+            try pq.add(food);
+            food = 0;
+            continue;
+        };
+        food += x;
+    }
+
+    var max3food: i32 = 0;
+    max3food += pq.remove();
+    std.debug.print("{}\n", .{max3food});
+    max3food += pq.remove();
+    max3food += pq.remove();
+    std.debug.print("{}\n", .{max3food});
+}