Advent of Code

Changes On Branch 85dc24a9dca42433
Login

Changes On Branch 85dc24a9dca42433

Changes In Branch TextGrid Through [85dc24a9dc] Excluding Merge-Ins

This is equivalent to a diff from 4ad4c64403 to 85dc24a9dc

2024-12-08
13:52
TextGrid looks ok check-in: d59f9c2e2f user: nnz tags: TextGrid
13:25
removed extra stuff check-in: 85dc24a9dc 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 [a12f3ca546].


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
24
25
26
27
28
29
#include <stbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "aocutils.h"

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

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 (row >= tg->rows) return NULL;
    if (col >= tg->cols) return NULL;
    return 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