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); } } var won = @splat(100, false); 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]; won[j] = for (b) |v| { if (@reduce(.Max, v) == 0) break true; } else @reduce(.Min, sum) == 0; if (@reduce(.And, won)) return print("{}\n", .{ (@reduce(.Add, sum) - u) * (n - 1) }); } } }