~cricket/advent2020

1a42d344c5908f8b5229143a897e2c38dee5b9be — cricket 2 months ago 4e22035
day 5: clean-up
1 files changed, 7 insertions(+), 29 deletions(-)

M 5/main.zig
M 5/main.zig => 5/main.zig +7 -29
@@ 5,39 5,20 @@ const fmt = std.fmt;

const print = std.debug.print;

fn array_list(a: *mem.Allocator) !std.ArrayList([]const u8) {
fn run(a: *mem.Allocator) [2]usize {
    var strings = mem.split(input, "\n");
    var list = std.ArrayList([]const u8).init(a);
    while (strings.next()) |string| {
        try list.append(string);
    }
    return list;
}

fn divCeil(x: usize, y: usize) usize {
    return (x/y) + @boolToInt(x%y != 0);
}

pub fn parts(slice: [][]const u8, a: *mem.Allocator) [2]usize {
    var ids = std.AutoHashMap(usize, bool).init(a);
    var max: usize = 0;
    for (slice) |item| {
        var row: [2]usize = .{0, 127};
        var column: [2]usize = .{0, 7};
    while (strings.next()) |item| {
        var id: usize = 0;
        for (item) |c| {
            switch (c) {
                'F' => row[1] = @divFloor(row[1] + row[0], 2),
                'B' => row[0] = divCeil(row[1] + row[0], 2),
                'L' => column[1] = @divFloor(column[1] + column[0], 2),
                'R' => column[0] = divCeil(column[1] + column[0], 2),
                else => unreachable
            }
            id <<= 1;
            id += @boolToInt(c == 'B' or c == 'R');
        }
        var id = row[0] * 8 + column[1];
        ids.put(id, true) catch unreachable;
        if (id > max) max = id;
    }
    var i: usize = max;
    var i = max;
    while (i > 0) : ( i -= 1 ) {
        _ = ids.get(i) orelse return .{max, i};
    }


@@ 47,9 28,6 @@ pub fn parts(slice: [][]const u8, a: *mem.Allocator) [2]usize {
pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const a = &gpa.allocator;
    var list = try array_list(a);
    const slice = list.items;

    var p = parts(slice, a);
    var p = run(a);
    print("part 1: {}\npart 2: {}\n", .{p[0], p[1]});
}
\ No newline at end of file