~rootmos/AoC

4051ab6be3a47bca6286259d08cb0aa75410d7d9 — Gustav Behm 11 days ago fcb3dd6
Add solution to AoC 2023 #13b
2 files changed, 24 insertions(+), 13 deletions(-)

M 2023/13.hs
M 2023/13.hs.c
M 2023/13.hs => 2023/13.hs +2 -0
@@ 43,3 43,5 @@ main :: IO ()
main = with parser $ do
  runC renderC "13.hs.c" "13.example" "a example"
  runC renderC "13.hs.c" "13.input" "a input"
  runC renderC "13.hs.c" "13.example" "b example"
  runC renderC "13.hs.c" "13.input" "b input"

M 2023/13.hs.c => 2023/13.hs.c +22 -13
@@ 8,6 8,7 @@ static int check_reflection_vertical(int n, int c)
    int X = size[n][0];
    int Y = size[n][1];

    int errors = 0;
    for(int y = 0; y < Y; y++) {
        for(int i = 0; i < X; i++) {
            int a = c-i;


@@ 16,12 17,12 @@ static int check_reflection_vertical(int n, int c)
            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;
                errors += 1;
            }
        }
    }

    return 1;
    return errors;
}

static int check_reflection_horizontal(int n, int r)


@@ 29,6 30,7 @@ static int check_reflection_horizontal(int n, int r)
    int X = size[n][0];
    int Y = size[n][1];

    int errors = 0;
    for(int x = 0; x < X; x++) {
        for(int i = 0; i < Y; i++) {
            int a = r-i;


@@ 37,29 39,29 @@ static int check_reflection_horizontal(int n, int r)
            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;
                errors += 1;
            }
        }
    }

    return 1;
    return errors;
}

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

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

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


@@ 83,13 85,20 @@ static void draw_pattern(int n)

int main(int argc, char* argv[])
{
    int sum = 0;
    for(int n = 0; n < N; n++) {
        /*draw_pattern(n);*/
        sum += find_reflection(n);
    if(argv[1][0] == 'a') {
        int sum = 0;
        for(int n = 0; n < N; n++) {
            /*draw_pattern(n);*/
            sum += find_reflection(n, 0);
        }
        printf("%s: %d\n", argv[1], sum);
    } else if(argv[1][0] == 'b') {
        int sum = 0;
        for(int n = 0; n < N; n++) {
            sum += find_reflection(n, 1);
        }
        printf("%s: %d\n", argv[1], sum);
    }

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

    return 0;
}