about summary refs log tree commit diff
path: root/aoc/2021/03/part-one.zig
blob: 6088925c36bbe18413ab3f99115038c2a725ebb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const print = @import("std").debug.print;

const input = @embedFile("input");
const mean = @splat(12, @as(u16, '0' * 1000 + 500));
const pos = @Vector(12, u4){ 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };

pub fn main() !void {
    var sum = @splat(12, @as(u16, 0));
    var i = @as(usize, 0);
    while (i < input.len) : (i += 13) {
        var j = @as(usize, 0);
        while (j < 12) : (j += 1)
            sum[j] += input[i + j];
    }

    const common = @bitCast(@Vector(12, u1), sum > mean);
    const gamma = @reduce(.Add, @as(@Vector(12, u12), common) << pos);
    print("{}\n", .{ @as(usize, gamma) * ~gamma });
}