about summary refs log tree commit diff
path: root/aoc/2021/04/part-one.zig
blob: 67dc4e0f12911ef22a681f0405c486688c3e3761 (plain) (blame)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const parseUnsigned = std.fmt.parseUnsigned;
const print = std.debug.print;
const std = @import("std");
const tokenize = std.mem.tokenize;

pub fn main() !void {
    var boards: [100][5]@Vector(5, u16) = undefined;
    var input = tokenize(@embedFile("input"), "\n");
    var order = tokenize(input.next().?, ",");
    var i = @as(usize, 0);
    while (i < 100) : (i += 1) {
        var j = @as(usize, 0);
        while (j < 5) : (j += 1) {
            var row = tokenize(input.next().?, " ");
            var k = @as(usize, 0);
            while (k < 5) : (k += 1)
                boards[i][j][k] = 1 + try parseUnsigned(u16, row.next().?, 10);
        }
    }

    const result = loop: while (i > 0) : (i -= 1) {
        const n = 1 + try parseUnsigned(u16, order.next().?, 10);
        var j = @as(usize, 0);
        while (j < 100) : (j += 1) {
            var u = @as(usize, 0);
            var k = @as(usize, 0);
            while (k < 5) : (k += 1) {
                var l = @as(usize, 0);
                while (l < 5) : (l += 1) {
                    if (boards[j][k][l] == n)
                        boards[j][k][l] = 0
                    else if (boards[j][k][l] > 0)
                        u += 1;
                }
            }

            const b = boards[j];
            const sum = b[0] + b[1] + b[2] + b[3] + b[4];
            const result = (@reduce(.Add, sum) - u) * (n - 1);
            if (@reduce(.Min, sum) == 0)
                break :loop result;
            for (b) |v|
                if (@reduce(.Max, v) == 0)
                    break :loop result;
        }
    } else unreachable;
    print("{}\n", .{ result });
}