~cricket/advent2020

e66899acef13987801133a122cd566d8470a2270 — cricket 9 months ago 9ab808a
day 4: small code cleanup?

* this isn't that much better, i did it immediately after finishing the puzzle and will probably clean it up a lot more later
1 files changed, 53 insertions(+), 42 deletions(-)

M 4/main.zig
M 4/main.zig => 4/main.zig +53 -42
@@ 19,56 19,67 @@ pub fn main() !void {
    const a = &gpa.allocator;
    var list = try array_list(a);
    const slice = list.items;
    
    var validp1: usize = 0;
    var v: usize = 0;

    var valid1: usize = 0;
    var valid2: usize = 0;

    for (slice) |entry| {
        var split = mem.tokenize(entry, "\n ");
        var valid: usize = 0;
        var vp1: usize = 0;
        var map = std.StringHashMap([]const u8).init(a);
        while (split.next()) |s| {
            var colon_split = mem.tokenize(s, ":");
            const field = colon_split.next().?;
            const value = colon_split.next().?;
            if (mem.eql(u8, field, "byr")) blk: {
                vp1 += 1;
                const val = fmt.parseInt(usize, value, 0) catch break :blk;
                valid += @boolToInt(val >= 1920 and val <= 2002);
            } else if (mem.eql(u8, field, "iyr")) blk: {
                vp1 += 1;
                const val = fmt.parseInt(usize, value, 0) catch break :blk;
                valid += @boolToInt(val >= 2010 and val <= 2020);
            } else if (mem.eql(u8, field, "eyr")) blk: {
                vp1 += 1;
                const val = fmt.parseInt(usize, value, 0) catch break :blk;
                valid += @boolToInt(val >= 2020 and val <= 2030);
            } else if (mem.eql(u8, field, "hgt")) {
                vp1 += 1;
                const ms = value[value.len-2..value.len];
                if (mem.eql(u8, ms, "cm")) blk: {
                    const val = value[0..value.len-2];
                    const num = fmt.parseInt(usize, val, 0) catch break :blk;
                    valid += @boolToInt(num >= 150 and num <= 193);
                } else if (mem.eql(u8, ms, "in")) blk: {
                    const val = value[0..value.len-2];
                    const num = fmt.parseInt(usize, val, 0) catch break :blk;
                    valid += @boolToInt(num >= 59 and num <= 76);
                }
            } else if (mem.eql(u8, field, "hcl")) {
                vp1 += 1;
                valid += @boolToInt(value[0] == '#' and value.len == 7);
            } else if (mem.eql(u8, field, "ecl")) {
                vp1 += 1;
                valid += @boolToInt(mem.eql(u8, value, "amb") or mem.eql(u8, value, "blu") or mem.eql(u8, value, "brn") or mem.eql(u8, value, "gry") or mem.eql(u8, value, "grn") or mem.eql(u8, value, "hzl") or mem.eql(u8, value, "oth"));
            } else if (mem.eql(u8, field, "pid")) blk: {
                vp1 += 1;
                _ = fmt.parseInt(usize, value, 0) catch break :blk;
                valid += @boolToInt(value.len == 9);
            try map.put(field, value);
        }

        var vie1: usize = 0;
        var vie: usize = 0;
        byr: {
            const value = map.get("byr") orelse break :byr; vie1 += 1;
            const num = fmt.parseInt(usize, value, 0) catch break :byr;
            vie += @boolToInt(num >= 1920 and num <= 2002);
        }
        iyr: {
            const value = map.get("iyr") orelse break :iyr; vie1 += 1;
            const num = fmt.parseInt(usize, value, 0) catch break :iyr;
            vie += @boolToInt(num >= 2010 and num <= 2020);
        }
        eyr: {
            const value = map.get("eyr") orelse break :eyr; vie1 += 1;
            const num = fmt.parseInt(usize, value, 0) catch break :eyr;
            vie += @boolToInt(num >= 2020 and num <= 2030);
        }
        hgt: {
            const value = map.get("hgt") orelse break :hgt; vie1 += 1;
            const ms = value[value.len-2..value.len];
            const val = value[0..value.len-2];
            if (mem.eql(u8, ms, "cm")) {
                const num = fmt.parseInt(usize, val, 0) catch break :hgt;
                vie += @boolToInt(num >= 150 and num <= 193);
            } else if (mem.eql(u8, ms, "in")) {
                const num = fmt.parseInt(usize, val, 0) catch break :hgt;
                vie += @boolToInt(num >= 59 and num <= 76);
            }
        }
        v += @boolToInt(valid >= 7);
        validp1 += @boolToInt(vp1 >= 7);
        hcl: {
            const value = map.get("hcl") orelse break :hcl; vie1 += 1;
            vie += @boolToInt(value[0] == '#' and value.len == 7);
        }
        ecl: {
            const value = map.get("ecl") orelse break :ecl; vie1 += 1;
            _ = mem.indexOf(u8, "amb blu brn gry grn hzl oth", value) orelse break :ecl;
            vie += 1;
        }
        pid: {
            const value = map.get("pid") orelse break :pid; vie1 += 1;
            _ = fmt.parseInt(usize, value, 0) catch break :pid;
            vie += @boolToInt(value.len == 9);
        }

        valid1 += @boolToInt(vie1 >= 7);
        valid2 += @boolToInt(vie >= 7);
    }

    print("part 1: {}\npart 2: {}\n", .{validp1, v});
    print("part 1: {}\npart 2: {}\n", .{valid1, valid2});
}
\ No newline at end of file