const std = @import("std");

const path = "data/day08/input.txt";

const RetType = u20;

const pattern_pieces = 10;
const output_pieces = 4;

const input_lines = 200;

const InputLine = struct {
    patterns: [pattern_pieces][]u8,
    outputs: [output_pieces][]u8,
    five_len: [3][]u8, // 2, 3, 5
    six_len: [3][]u8, // 0, 6, 9
};

const FullInput = [input_lines]InputLine;

fn parseInput() anyerror!FullInput {
    const input = @embedFile(path);
    var lines = std.mem.tokenize(u8, input, "\n");

    var inl: FullInput = undefined;

    // THIS IS THE RIGHT PLACE FOR THE BUFFER
    var buffer: [input_lines][output_pieces + pattern_pieces][7]u8 = undefined;

    var i: usize = 0;
    while (lines.next()) |line| : (i += 1) {
        var pattern_output = std.mem.tokenize(u8, line, "|");
        _ = std.mem.tokenize(u8, pattern_output.next().?, " ");
        var outs = std.mem.tokenize(u8, pattern_output.next().?, " ");

        // collect outs
        var idx: usize = 0;
        while (outs.next()) |out| : (idx += 1) {
            // THIS IS BAD: inl[i].outputs[idx] points to nowhere outside the block
            // var buffer: [7]u8 = undefined;
            // inl[i].outputs[idx] = buffer[0..out.len];
            inl[i].outputs[idx] = buffer[i][idx][0..out.len];
            std.mem.copy(u8, inl[i].outputs[idx], out);

            std.sort.sort(u8, inl[i].outputs[idx], {}, comptime std.sort.asc(u8));
        }

        // collect patterns (removed for brevity)
    }

    return inl;
}

pub fn main() anyerror!void {
    var input = try parseInput();

    for (input) |line| {
        for (line.outputs) |out| {
            std.debug.print("{s} ", .{out});
        }
        std.debug.print("\n", .{});
    }
}