advent-of-code

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

a.c (3558B)


      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
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define ROPELEN 10

#define UP(head)    ((head).y++)
#define DOWN(head)  ((head).y--)
#define LEFT(head)  ((head).x--)
#define RIGHT(head) ((head).x++)

struct point {
    long x, y;
};

struct visited {
    size_t len;
    struct point *p;
};

void *
emalloc(size_t size) {
    void *p = calloc(size, 1);
    if (p == NULL) {
        perror("emalloc");
        exit(1);
    }
    return p;
}

struct visited *
visited_init(void) {
    struct visited *v;
    v = emalloc(sizeof (struct visited));
    v->p = emalloc(sizeof (struct point));
    v->len = 1;
    v->p[0].x = v->p[0].y = 0;
    return v;
}

void
visited_destroy(struct visited *v) {
    if (v) {
        free(v->p);
        free(v);
    }
}

int
check_visited(struct point *p, struct visited *v) {
    for (size_t i = 0; i < v->len; i++) {
        if ((v->p[i].x == p->x) &&
                (v->p[i].y == p->y))
            return 1;
    }
    return 0;
}

void
visit(struct point *tail, struct visited **v) {
    if (check_visited(tail, *v) == 1)
        return;
    struct point *tmp = realloc((*v)->p, sizeof(struct point) * ((*v)->len + 1));
    if (tmp == NULL) {
        free((*v)->p);
        free(*v);
        perror("realloc");
        exit(1);
    }
    tmp[(*v)->len].x = tail->x;
    tmp[(*v)->len].y = tail->y;
    (*v)->p = tmp;
    (*v)->len++;
    return;
}

void
update_tail(struct point *h, struct point *t) {
    long dx = h->x - t->x;
    long dy = h->y - t->y;
    double d = sqrt((dx * dx) + (dy * dy));
    if (d < 2.0)
        return;

    if (h->x == t->x) {
        t->y += h->y > t->y ? 1 : -1;
    } else if (h->y == t->y) {
        t->x += h->x > t->x ? 1 : -1;
    } else {
        t->x += h->x > t->x ? 1 : -1;
        t->y += h->y > t->y ? 1 : -1;
    }
}

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

    struct visited *v1, *v2;
    struct point rope[ROPELEN] = {0};
    v1 = visited_init();
    v2 = visited_init();

    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;
    }

    while ((n = getline(&buf, &buflen, f)) != -1) {
        if (buf[n - 1] == '\n') {
            buf[n - 1] = '\0';
            n--;
        }
        int num;
        if (sscanf(buf+2, "%d", &num) != 1) {
            perror("sscanf");
            return 1;
        }
        for (int i = 0; i < num; i++) {
            switch (buf[0]) {
                case 'U':
                    UP(rope[0]);
                    break;
                case 'D':
                    DOWN(rope[0]);
                    break;
                case 'L':
                    LEFT(rope[0]);
                    break;
                case 'R':
                    RIGHT(rope[0]);
                    break;
                default:
                    fprintf(stderr, "unknown command \"%s\"\n", buf);
                    return 1;
            }

            for (size_t i = 1; i < ROPELEN; i++)
                update_tail(&rope[i - 1], &rope[i]);

            /* Keep track of the knot immediately after the head (A), and the last knot (B). */
            visit(&rope[1], &v1);
            visit(&rope[ROPELEN-1], &v2);
        }
    }

    printf( "Part A: %ld\n"
            "Part B: %ld\n",
            v1->len,
            v2->len);

    visited_destroy(v1);
    visited_destroy(v2);

    free(buf);
    fclose(f);

    return 0;
}