Advent of Code

Changes On Branch TextGrid
Login

Changes On Branch TextGrid

Changes In Branch TextGrid Excluding Merge-Ins

This is equivalent to a diff from 4ad4c64403 to d487b46741

2024-12-08
14:40
merge TextGrid check-in: 2f2a7645d9 user: nnz tags: trunk
13:54
added test file Leaf check-in: d487b46741 user: nnz tags: TextGrid
13:53
added test file check-in: 85a490fa64 user: nnz tags: TextGrid
13:22
202408 wip check-in: a382d65717 user: nnz tags: trunk
13:13
struct TextGrid is replacing linearize2d check-in: f28e83772a user: nnz tags: TextGrid
2024-12-07
14:41
fixed possible bug (3) check-in: 4ad4c64403 user: nnz tags: trunk
14:39
fixed possible bug (2) check-in: afa0bfe340 user: nnz tags: trunk

Modified aoc2024.c from [9685519f7e] to [bf5d44504f].

73
74
75
76
77
78
79

80
81
82
83
84
85
86

/* === aoc202406 =======================================================
   Hmmm, I've done something like this already this year... on day 4
   I need rcmap() moved to aocutils ... done; now it's called `linearize2d`
Part Two: brute force? It likely is faster than coming up with an algo
   Let's go: save map, for every available position put in an obstacle,
   loop around until some place is visited 4? times or area is exited

===================================================================== */

static bool validpos(unsigned s, int r, int c) {
    if (r < 0) return false;
    if (c < 0) return false;
    if ((unsigned)r >= s - 1) return false;
    if ((unsigned)c >= s - 1) return false;







>







73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

/* === aoc202406 =======================================================
   Hmmm, I've done something like this already this year... on day 4
   I need rcmap() moved to aocutils ... done; now it's called `linearize2d`
Part Two: brute force? It likely is faster than coming up with an algo
   Let's go: save map, for every available position put in an obstacle,
   loop around until some place is visited 4? times or area is exited
TODO remove linearize2d
===================================================================== */

static bool validpos(unsigned s, int r, int c) {
    if (r < 0) return false;
    if (c < 0) return false;
    if ((unsigned)r >= s - 1) return false;
    if ((unsigned)c >= s - 1) return false;
230
231
232
233
234
235
236

237
238
239
240
241
242
243
        accumsum += accum;
    } while ((line = strtok(NULL, "\n")) != NULL);
    printf("The sum of middle update numbers is {%u}.\n", accumsum);
    printf("The sum of middle update numbers for newly ordered updates is {%u}.\n", accumsum2);
}

/* === aoc202404 =======================================================

===================================================================== */

static bool masat(char *data, unsigned size, unsigned row, unsigned col, int drow, int dcol) {
    int maxrow = (int)row + 3*drow;
    int maxcol = (int)col + 3*dcol;
    if ((0 <= maxrow) && (maxrow < (int)size) && (0 <= maxcol) && (maxcol < (int)size)) {
        if (data[linearize2d(size+1, (unsigned)((int)row + drow), (unsigned)((int)col + dcol))] != 'M') return false;







>







231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
        accumsum += accum;
    } while ((line = strtok(NULL, "\n")) != NULL);
    printf("The sum of middle update numbers is {%u}.\n", accumsum);
    printf("The sum of middle update numbers for newly ordered updates is {%u}.\n", accumsum2);
}

/* === aoc202404 =======================================================
TODO remove linearize2d
===================================================================== */

static bool masat(char *data, unsigned size, unsigned row, unsigned col, int drow, int dcol) {
    int maxrow = (int)row + 3*drow;
    int maxcol = (int)col + 3*dcol;
    if ((0 <= maxrow) && (maxrow < (int)size) && (0 <= maxcol) && (maxcol < (int)size)) {
        if (data[linearize2d(size+1, (unsigned)((int)row + drow), (unsigned)((int)col + dcol))] != 'M') return false;

Modified aocutils.c from [68c37aa5e0] to [a7eed73da2].


1
2
3
4











5
6
7
8
9
10
11

#include <stdio.h>
#include <stdlib.h>
#include "aocutils.h"












size_t linearize2d(unsigned width, unsigned row, unsigned col) {
    return (row * width) + col;
}

size_t text2array(unsigned **dst, const char *r) {
    unsigned *a = malloc(512 * sizeof *a);
    size_t na = 0, sa = 512;
>




>
>
>
>
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "aocutils.h"

bool TGvalid(struct TextGrid *tg, unsigned row, unsigned col) {
    if (row >= tg->rows) return false;
    if (col >= tg->cols) return false;
    return true;
}
char *TGcharptr(struct TextGrid *tg, unsigned row, unsigned col) {
    if (!TGvalid(tg, row, col)) return NULL;
    return tg->data + (row * tg->cols) + col;
}

// TODO: rewrite this shit
size_t linearize2d(unsigned width, unsigned row, unsigned col) {
    return (row * width) + col;
}

size_t text2array(unsigned **dst, const char *r) {
    unsigned *a = malloc(512 * sizeof *a);
    size_t na = 0, sa = 512;

Modified aocutils.h from [475433f577] to [838e050968].

1
2
3








4
5
6
7
8
9
10
11
#ifndef AOCUTILS_H_INCLUDED
#define AOCUTILS_H_INCLUDED









size_t linearize2d(unsigned width, unsigned row, unsigned col);
size_t text2array(unsigned **dst, const char *txt);
size_t slurp(char **dst, const char *filename);
unsigned distance(unsigned a, unsigned b);
unsigned max3u(unsigned a, unsigned b, unsigned c);
unsigned min3u(unsigned a, unsigned b, unsigned c);

#endif



>
>
>
>
>
>
>
>
|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef AOCUTILS_H_INCLUDED
#define AOCUTILS_H_INCLUDED

struct TextGrid {
    unsigned rows, cols;
    char *data; // may have '\n' at end of rows
};

bool TGvalid(struct TextGrid *tg, unsigned row, unsigned col);
char *TGcharptr(struct TextGrid *tg, unsigned row, unsigned col);

size_t linearize2d(unsigned width, unsigned row, unsigned col); // TODO
size_t text2array(unsigned **dst, const char *txt);
size_t slurp(char **dst, const char *filename);
unsigned distance(unsigned a, unsigned b);
unsigned max3u(unsigned a, unsigned b, unsigned c);
unsigned min3u(unsigned a, unsigned b, unsigned c);

#endif

Added test.txt version [88af757c9f].

























>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

Added testtg.c version [d61466e43e].





















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include "aocutils.h"

int main(void) {
    char *input = NULL;
    size_t ilen = slurp(&input, "test.txt");

    struct TextGrid tg;
    tg.data = input;
    tg.cols = strchr(input, '\n') - input + 1;
    tg.rows = tg.cols - 1; // rows includes the '\n'

    if (*TGcharptr(&tg, 11, 11) == '.') puts("1");
    if (TGcharptr(&tg, 12, 11) == NULL) puts("2");
    if (TGcharptr(&tg, 11, 12) == NULL) puts("3");
    if (*TGcharptr(&tg, 11, 12) == '\n') puts("33");
    if (TGcharptr(&tg, -1, -2) == NULL) puts("4");

    for (int k = 0; k < 12; k++) {
        *TGcharptr(&tg, k, k) = '#';
    }
    printf("\n\n%s\n", tg.data);
}