Advent of Code

Check-in [aca10ca251]
Login

Check-in [aca10ca251]

Overview
Comment:ready for brute force
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: aca10ca25102c53319f419c5efd1d55f333797b8df079b4a3c53666561ba4e31
User & Date: nnz on 2024-12-06 13:49:29
Other Links: manifest | tags
Context
2024-12-06
13:54
add space for terminating zero check-in: 16cb8577e3 user: nnz tags: trunk
13:49
ready for brute force check-in: aca10ca251 user: nnz tags: trunk
12:56
202406 1st star check-in: a114787839 user: nnz tags: trunk
Changes

Modified aoc2024.c from [445ebe9c15] to [02118ec5ef].

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
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"

/* === 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`



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

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

static void rightturn(int *drow, int *dcol) {
    if (*drow) { *dcol = -*drow; *drow = 0; }
    else       { *drow =  *dcol; *dcol = 0; }
}

void aoc202406(char *data, size_t len) {
    (void)len;

    // assume data is well-formatted and has the same number of rows and columns
    unsigned size = 1 + strchr(data, '\n') - data;
    unsigned linear_pos = strchr(data, '^') - data;
    int row_pos = (int)(linear_pos / size);
    int col_pos = (int)(linear_pos % size);
    int deltarow = -1, deltacol = 0;
    //printf("character at [%u, %u] is '%c'\n", row_pos, col_pos, data[linearize2d(size, row_pos, col_pos)]);












>
>
>
















|
>







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
#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"

/* === 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;
    return true;
}

static void rightturn(int *drow, int *dcol) {
    if (*drow) { *dcol = -*drow; *drow = 0; }
    else       { *drow =  *dcol; *dcol = 0; }
}

void aoc202406(char *data, size_t len) {
    char *savedmap = malloc(len + 1);
    strcpy(savedmap, data);
    // assume data is well-formatted and has the same number of rows and columns
    unsigned size = 1 + strchr(data, '\n') - data;
    unsigned linear_pos = strchr(data, '^') - data;
    int row_pos = (int)(linear_pos / size);
    int col_pos = (int)(linear_pos % size);
    int deltarow = -1, deltacol = 0;
    //printf("character at [%u, %u] is '%c'\n", row_pos, col_pos, data[linearize2d(size, row_pos, col_pos)]);
45
46
47
48
49
50
51


52
53
54
55
56
57
58
                      /*fallthrough*/
            case 'X': row_pos += deltarow;
                      col_pos += deltacol;
                      break;
        }
    }
    printf("The guard visits %u positions before leaving the area.\n", visited);


}

/* === aoc202405 =======================================================
===================================================================== */

static int ppcmp(unsigned a, unsigned b, unsigned (*r)[2], size_t nr) {
    for (size_t k = 0; k < nr; k++) {







>
>







49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
                      /*fallthrough*/
            case 'X': row_pos += deltarow;
                      col_pos += deltacol;
                      break;
        }
    }
    printf("The guard visits %u positions before leaving the area.\n", visited);
    // Part Two
    free(savedmap);
}

/* === aoc202405 =======================================================
===================================================================== */

static int ppcmp(unsigned a, unsigned b, unsigned (*r)[2], size_t nr) {
    for (size_t k = 0; k < nr; k++) {