Advent of Code

Diff
Login

Diff

Differences From Artifact [2fd9fa9d99]:

To Artifact [9332948c9c]:


1
2
3
4
5
6




















































7
8
9
10
11
12
13
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






+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocutils.h"

char RMcharat(struct RectangularMap *sm, int col, int row) {
    if (col < 0) return 0;
    if (row < 0) return 0;
    if (col >= sm->cols) return 0;
    if (row >= sm->rows) return 0;
    return sm->data[row*sm->cols + col];
}

char *RMcharptr(struct RectangularMap *sm, int col, int row) {
    if (col < 0) return NULL;
    if (row < 0) return NULL;
    if (col >= sm->cols) return NULL;
    if (row >= sm->rows) return NULL;
    return sm->data + row*sm->cols + col;
}

void RMcopy(struct RectangularMap *restrict dst,
            const struct RectangularMap *restrict src) {
    size_t size = (size_t)src->cols * (size_t)src->rows;
    char *tmp = realloc(dst->data, size);
    if (tmp == NULL) {
        fprintf(stderr, "not enough memory\n");
        exit(EXIT_FAILURE);
    }
    dst->data = tmp;
    memcpy(dst->data, src->data, size);
}

void RMaddline(struct RectangularMap *sm, const char *lin) {
    int linlen = strlen(lin);
    if (sm->cols && (sm->cols != linlen)) {
        // error
        fprintf(stderr, "wrong line length!\n");
        exit(EXIT_FAILURE);
    }
    if (sm->cols == 0) sm->cols = linlen;
    char *tmp = realloc(sm->data, (size_t)(sm->rows+1) * (size_t)sm->cols);
    if (tmp == NULL) {
        fprintf(stderr, "not enough memory\n");
        exit(EXIT_FAILURE);
    }
    sm->data = tmp;
    memcpy(sm->data + sm->rows*sm->cols, lin, (size_t)sm->cols);
    sm->rows += 1;
}

void RMfree(struct RectangularMap *sm) {
    free(sm->data);
    sm->data = NULL;
    sm->cols = sm->rows = 0;
}

static uint32_t f(uint32_t x, uint32_t y, uint32_t z) {
    return (x & y) | (~x & z);
}

static uint32_t g(uint32_t x, uint32_t y, uint32_t z) {
    return (x & z) | (y & ~z);