#import "Basic";
#import "File";
#import "String";
#import "Sort";
#import "Math";

solve_day11 :: (test: bool) {
    contents := read_entire_file("inputs/day11.txt");
    lines := split(contents, "\n");

    has_in_row   : [..]bool;
    defer array_free(has_in_row);
    has_in_column: [..]bool;
    defer array_free(has_in_column);

    galaxy_positions: [..]Coord;
    defer array_free(galaxy_positions);

    for 0..lines[0].count - 1 {
        array_add(*has_in_column, false);
    }
    for line, line_index: lines {
        found_galaxy := false;

        for i: 0..line.count - 1 {
            if line[i] == #char "#" {
                has_in_column[i] = true;
                found_galaxy = true;
                array_add(*galaxy_positions, .{
                    x = i,
                    y = line_index,
                    x2 = i,
                    y2 = line_index,
                });

            }
        }

        array_add(*has_in_row, found_galaxy);
    }

    expand_galaxies(galaxy_positions, has_in_row, has_in_column, 1, 999_999);

    part1 := 0;
    part2 := 0;

    for position, position_index: galaxy_positions {
        for position2: array_view(galaxy_positions, position_index, galaxy_positions.count) {
            part1 += abs(position2.x - position.x) + abs(position2.y - position.y);
            part2 += abs(position2.x2 - position.x2) + abs(position2.y2 - position.y2);
        }
    }

    print("Part 1: %\n", part1);
    print("Part 2: %\n", part2);
}

expand_galaxies :: (galaxies: []Coord, has_in_row: [..]bool, has_in_column: [..]bool, expand_with: s64, expand_with2: s64) {
    for < i: has_in_row.count - 1..0 {
        if !has_in_row[i] {
            for *position: galaxies {
                if position.y > i {
                    position.y += expand_with;
                    position.y2 += expand_with2;
                }
            }
        }
    }

    for < i: has_in_column.count - 1..0 {
        if !has_in_column[i] {
            for *position: galaxies {
                if position.x > i {
                    position.x += expand_with;
                    position.x2 += expand_with2;
                }
            }
        }
    }
}
#scope_file
Coord :: struct {
    x: s64;
    y: s64;
    x2: s64;
    y2: s64;
}