advent-of-code

advent of code attempts
git clone git://bvnf.space/advent-of-code.git
Log | Files | Refs

a.c (3450B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIDE 99U

int
main(int argc, char **argv) {
    char *buf = NULL;
    size_t buflen = 0;
    ssize_t n;
    FILE *f;

    char grid[SIDE][SIDE] = {0};

    if (argc != 2) {
        fprintf(stderr, "usage: %s input\n", argv[0]);
        return 1;
    }

    f = fopen(argv[1], "r");
    if (f == NULL) {
        perror(argv[1]);
        return 1;
    }

    size_t row = 0;
    while ((n = getline(&buf, &buflen, f)) != -1) {
        if (buf[n - 1] == '\n') {
            buf[n - 1] = '\0';
            n--;
        }
        assert(n == SIDE);
        assert(row < SIDE);
        memcpy(grid[row++], buf, n);
    }

    free(buf);
    fclose(f);

    int total = 0;
    for (size_t i = 0; i < SIDE; i++) {
        for (size_t j = 0; j < SIDE; j++) {
            
            char me = grid[i][j];
            char visl, visr, visu, visd;
            visl = visr = visu = visd = 1;

            /* wow, this is awful */
            for (size_t k = 0; k < i; k++) {
                if (grid[k][j] >= me) {
                    visu = 0;
                    break;
                }
            }
            if (visu)
                goto visible; /* an optimisation! */
            for (size_t k = i + 1; k < SIDE; k++) {
                if (grid[k][j] >= me) {
                    visd = 0;
                    break;
                }
            }
            if (visd)
                goto visible;
            for (size_t l = 0; l < j; l++) {
                if (grid[i][l] >= me) {
                    visl = 0;
                    break;
                }
            }
            if (visl)
                goto visible;
            for (size_t l = j + 1; l < SIDE; l++) {
                if (grid[i][l] >= me) {
                    visr = 0;
                    break;
                }
            }

            if (visu || visd || visl || visr) {
visible:
                total++;
            }

        }
    }

    printf("Part A: %d\n", total);

    int max = 0;
    for (size_t i = 0; i < SIDE; i++) {
        for (size_t j = 0; j < SIDE; j++) {
            
            total = 0;
            char me = grid[i][j];
            int visl, visr, visu, visd;
            visl = visr = visu = visd = 0;

            /* lazy but it works */
            for (int k = i - 1; k >= 0; k--) {
                visu++;
                if (grid[k][j] >= me)
                    break;
            }
            if (visu == 0)
                continue;
            for (size_t k = i + 1; k < SIDE; k++) {
                visd++;
                if (grid[k][j] >= me)
                    break;
            }
            if (visd == 0)
                continue;
            for (int l = j - 1; l >= 0; l--) {
                visl++;
                if (grid[i][l] >= me)
                    break;
            }
            if (visl == 0)
                continue;
            for (size_t l = j + 1; l < SIDE; l++) {
                visr++;
                if (grid[i][l] >= me)
                    break;
            }

            total = visu * visd * visl * visr;
            if (total > max) {
                /*fprintf(stderr, "%d -> %d at [%lu,%lu] = %c sees [%d,%d,%d,%d]\n",
                 * max, total, i, j, me, visu, visr, visd, visl);*/
                max = total;
            }

        }
    }

    printf("Part B: %d\n", max);

    return 0;
}