Advent of Code

Check-in [c93635c614]
Login

Check-in [c93635c614]

Overview
Comment:changed empty values to work with part two
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c93635c614757d7347a13a91a745d6437c6566de9f614cc612fa8981e0412177
User & Date: nnz on 2024-12-09 12:24:52
Other Links: manifest | tags
Context
2024-12-09
12:52
changed empty values again to work with part two check-in: cf1a1232c2 user: nnz tags: trunk
12:24
changed empty values to work with part two check-in: c93635c614 user: nnz tags: trunk
11:50
202409 1st star check-in: 8682072363 user: nnz tags: trunk
Changes

Modified aoc2024.c from [3ba7100c1a] to [694ae36748].

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
        if (*data) {
            for (int k = 0; k < *data - '0'; k++) {
                if (ndisk == rdisk) {
                    // assume it works
                    rdisk = (13*rdisk)/8;
                    disk = realloc(disk, rdisk * sizeof *disk);
                }
                disk[ndisk++] = -1;
            }
            data++;
        }
    }




    int *left = disk, *right = disk + ndisk;
    for (;;) {
        while (*left != -1) left++;
        if (left >= right) break;
        while (right[-1] == -1) right--;
        // swap *left and right[-1]
        int tmp = *--right;
        *right = *left;
        *left++ = tmp;
    }
    unsigned long long chksum = 0;
    size_t index = 0;
    while (disk[index] >= 0) {
        chksum += (size_t)disk[index] * index;
        index++;
    }
    printf("%llu\n", chksum);
    free(disk);



}

/* === aoc202408 =======================================================
   Oh! another square of text!
   Idea: for all points p with an antenna find all points q>p with
a corresponding frequency. For each such pair calculate the 2 antinodes
and add the resulting points (if not there already) to an array.







|




>
>
>
>


|

|













>
>
>







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
        if (*data) {
            for (int k = 0; k < *data - '0'; k++) {
                if (ndisk == rdisk) {
                    // assume it works
                    rdisk = (13*rdisk)/8;
                    disk = realloc(disk, rdisk * sizeof *disk);
                }
                disk[ndisk++] = -1 * (*data - '0');
            }
            data++;
        }
    }
    // copy disk
    int *diskcopy = malloc(ndisk * sizeof *disk);
    memcpy(diskcopy, disk, ndisk * sizeof *disk);

    int *left = disk, *right = disk + ndisk;
    for (;;) {
        while (*left >= 0) left++;
        if (left >= right) break;
        while (right[-1] < 0) right--;
        // swap *left and right[-1]
        int tmp = *--right;
        *right = *left;
        *left++ = tmp;
    }
    unsigned long long chksum = 0;
    size_t index = 0;
    while (disk[index] >= 0) {
        chksum += (size_t)disk[index] * index;
        index++;
    }
    printf("%llu\n", chksum);
    free(disk);

    // part 2
    free(diskcopy);
}

/* === aoc202408 =======================================================
   Oh! another square of text!
   Idea: for all points p with an antenna find all points q>p with
a corresponding frequency. For each such pair calculate the 2 antinodes
and add the resulting points (if not there already) to an array.