Advent of Code

Check-in [8682072363]
Login

Check-in [8682072363]

Overview
Comment:202409 1st star
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 868207236374b9594ad435b7a82432c0e27d575566079ad795f9ea7c3edf6a3d
User & Date: nnz on 2024-12-09 11:50:44
Other Links: manifest | tags
Context
2024-12-09
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
2024-12-08
17:11
202408 2nd star check-in: 9395df6993 user: nnz tags: trunk
Changes

Modified aoc2024.c from [05505307b8] to [3ba7100c1a].

1
2
3
4
5
6
7
8





















































9
10
11
12
13
14
15
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
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








+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







#include <ctype.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aocdailies.h"
#include "aocutils.h"

/* === aoc202409 =======================================================
   WOW! input consists of a 20,000 long string of numbers!
===================================================================== */

void aoc202409(char *data, size_t len) {
    (void)len; // unused argument
    int *disk = malloc(512 * sizeof *disk);
    size_t rdisk = 512;
    size_t ndisk = 0;
    int fileid = 0;
    while (*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++] = fileid;
        }
        fileid++;
        data++;
        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.
   The answer to Part One is the number of elements in the array

Modified aocdailies.c from [fb8ca294f1] to [53d57bd116].

1
2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17









+







#include <stddef.h>
#include "aocdailies.h"

aocfunc *aocselect(unsigned y, unsigned d) {
    aocfunc *p;
    switch (y * 100 + d) {
        default: p = NULL; break;

        //   YYYYdd ==>  aocYYYYdd
        case 202409: p = aoc202409; break;
        case 202408: p = aoc202408; break;
        case 202407: p = aoc202407; break;
        case 202406: p = aoc202406; break;
        case 202405: p = aoc202405; break;
        case 202404: p = aoc202404; break;
        case 202403: p = aoc202403; break;
        case 202402: p = aoc202402; break;

Modified aocdailies.h from [114faad3c2] to [e29d12fccf].

1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16








+







#ifndef AOCDAILIES_H_INCLUDED
#define AOCDAILIES_H_INCLUDED

#include <stddef.h>

typedef void aocfunc(char *, size_t);
aocfunc *aocselect(unsigned, unsigned);

aocfunc aoc202409;
aocfunc aoc202408;
aocfunc aoc202407;
aocfunc aoc202406;
aocfunc aoc202405;
aocfunc aoc202404;
aocfunc aoc202403;
aocfunc aoc202402;