~cricket/advent2020

ref: 1a42d344c5908f8b5229143a897e2c38dee5b9be advent2020/5/main.zig -rw-r--r-- 857 bytes
1a42d344cricket day 5: clean-up 10 months ago
                                                                                
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 input = @embedFile("input");
const mem = std.mem;
const fmt = std.fmt;

const print = std.debug.print;

fn run(a: *mem.Allocator) [2]usize {
    var strings = mem.split(input, "\n");
    var ids = std.AutoHashMap(usize, bool).init(a);
    var max: usize = 0;
    while (strings.next()) |item| {
        var id: usize = 0;
        for (item) |c| {
            id <<= 1;
            id += @boolToInt(c == 'B' or c == 'R');
        }
        ids.put(id, true) catch unreachable;
        if (id > max) max = id;
    }
    var i = max;
    while (i > 0) : ( i -= 1 ) {
        _ = ids.get(i) orelse return .{max, i};
    }
    return .{0, 0};
}

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const a = &gpa.allocator;
    var p = run(a);
    print("part 1: {}\npart 2: {}\n", .{p[0], p[1]});
}