Advent of Code

Diff
Login

Diff

Differences From Artifact [3ba7100c1a]:

To Artifact [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.