about summary refs log tree commit diff
path: root/day01.zig
blob: a12204cec1d28b6e8e04b06c0c383fe656b7bb38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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});
}