~rootmos/AoC

fcb3dd6ad6e17d1bea075dbe902fd1f8652f696f — Gustav Behm 11 days ago 74599e0
Add solution to AoC 2023 #13a
1 files changed, 83 insertions(+), 13 deletions(-)

M 2023/13.hs.c
M 2023/13.hs.c => 2023/13.hs.c +83 -13
@@ 1,25 1,95 @@
#include <stdio.h>
#define LIBR_IMPLEMENTATION
#include <r.h>

#include "input.h"

int main(int argc, char* argv[])
static int check_reflection_vertical(int n, int c)
{
    printf("%s:\n", argv[1]);
    int X = size[n][0];
    int Y = size[n][1];

    for(int n = 0; n < N; n++) {
        printf("%dx%d\n", size[n][0], size[n][1]);
        for(int y = 0; y < size[n][1]; y++) {
            for(int x = 0; x < size[n][0]; x++) {
                if(mirror[n][y][x]) {
                    printf("#");
                } else {
                    printf(".");
                }
    for(int y = 0; y < Y; y++) {
        for(int i = 0; i < X; i++) {
            int a = c-i;
            if(a < 0) continue;
            int b = c+1+i;
            if(b >= X) continue;
            /*printf("%d %d: %d %d\n", (a+1), (b+1), mirror[n][y][a], mirror[n][y][b]);*/
            if(mirror[n][y][a] != mirror[n][y][b]) {
                return 0;
            }
        }
    }

    return 1;
}

static int check_reflection_horizontal(int n, int r)
{
    int X = size[n][0];
    int Y = size[n][1];

    for(int x = 0; x < X; x++) {
        for(int i = 0; i < Y; i++) {
            int a = r-i;
            if(a < 0) continue;
            int b = r+1+i;
            if(b >= Y) continue;
            /*printf("%d %d: %d %d\n", (a+1), (b+1), mirror[n][a][x], mirror[n][b][x]);*/
            if(mirror[n][a][x] != mirror[n][b][x]) {
                return 0;
            }
        }
    }

    return 1;
}

static int find_reflection(int n)
{
    int X = size[n][0];
    int Y = size[n][1];

    int v = 0;
    for(; v < X-1; v++) {
        if(check_reflection_vertical(n, v)) {
            return (v+1);
        }
    }

    int r = 0;
    for(; r < Y-1; r++) {
        if(check_reflection_horizontal(n, r)) {
            return (r+1)*100;
        }
    }

    failwith("unreachable?");
}

static void draw_pattern(int n)
{
    for(int y = 0; y < size[n][1]; y++) {
        for(int x = 0; x < size[n][0]; x++) {
            if(mirror[n][y][x]) {
                printf("#");
            } else {
                printf(".");
            }
            printf("\n");
        }
        printf("\n");
    }
}

int main(int argc, char* argv[])
{
    int sum = 0;
    for(int n = 0; n < N; n++) {
        /*draw_pattern(n);*/
        sum += find_reflection(n);
    }

    printf("%s: %d\n", argv[1], sum);

    return 0;
}