aoc-2020-zig

Advent of Code 2020 Solutions in Zig
git clone https://git.sinitax.com/sinitax/aoc-2020-zig
Log | Files | Refs | README | sfeed.txt

main.zig (1170B)


      1const std = @import("std");
      2const aoc = @import("aoc");
      3
      4fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 {
      5    _ = args;
      6
      7    var entryit = std.mem.split(u8, input, "\n\n");
      8    var answer: u32 = 0;
      9    while (entryit.next()) |group| {
     10        var seen = [_]u1{0} ** 256;
     11        for (group) |c| {
     12            if (c == ' ' or c == '\n') continue;
     13            answer += @boolToInt(seen[c] == 0);
     14            seen[c] = 1;
     15        }
     16    }
     17
     18    return try std.fmt.allocPrint(allocator, "{d}", .{answer});
     19}
     20
     21fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 {
     22    _ = args;
     23
     24    var entryit = std.mem.split(u8, input, "\n\n");
     25    var answer: u32 = 0;
     26    while (entryit.next()) |group| {
     27        var count = [_]u16{0} ** 256;
     28        var members: u16 = 0;
     29        var memberit = std.mem.tokenize(u8, group, "\n ");
     30        while (memberit.next()) |member| : (members += 1) {
     31            for (member) |c| count[c] += 1;
     32        }
     33        for (count) |v| answer += @boolToInt(v == members);
     34    }
     35
     36    return try std.fmt.allocPrint(allocator, "{d}", .{answer});
     37}
     38
     39pub const main = aoc.main(part1, part2, .{ "6748", "3445" });