Overview
Comment: | 202409 2nd star |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
f303335c22e1c9e68d5467dd70c1f9b8 |
User & Date: | nnz on 2024-12-09 18:06:52 |
Other Links: | manifest | tags |
Context
2024-12-15
| ||
11:18 | 201504 two stars check-in: 17bcf7e37c user: nnz tags: trunk | |
2024-12-11
| ||
14:28 | I have been working on this for some time check-in: 235f764371 user: nnz tags: md5mini | |
2024-12-09
| ||
18:06 | 202409 2nd star check-in: f303335c22 user: nnz tags: trunk | |
12:52 | changed empty values again to work with part two check-in: cf1a1232c2 user: nnz tags: trunk | |
Changes
Modified aoc2024.c from [1b7a71a647] to [1416b27373].
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 | #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); } | > > > | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #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! Make an array of blocks, each with either -1 or the fileid For Part Two: make an array of blocks with the encoded value of (fileid * 10) + length, or, when empty, the negative length ===================================================================== */ 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 * 10) + (*data - '0'); // encoded } 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 - '0'); // negative length } data++; } } // copy disk for Part Two 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--; if (left >= right) break; // 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]/10) * index; index++; } printf("%llu\n", chksum); free(disk); // part 2 for (int filetomove = fileid - 1; filetomove > 0; filetomove--) { int *startoffile = diskcopy; while (*startoffile / 10 != filetomove) { if (*startoffile < 0) startoffile += -*startoffile; else startoffile += *startoffile % 10; } int blocksneeded = *startoffile % 10; int *bigblock = diskcopy; while (bigblock < startoffile) { if (*bigblock < 0) { if (-*bigblock < blocksneeded) { bigblock += -*bigblock; } else { // found a suitable block: move and quit tightest loop int bbsize = -*bigblock; int k; for (k = 0; k < blocksneeded; k++) { int tmp = startoffile[k]; startoffile[k] = bigblock[k]; bigblock[k] = tmp; } for (; k < bbsize; k++) { bigblock[k] += blocksneeded; } break; } } else { bigblock += *bigblock % 10; } } } unsigned long long chksum2 = 0; index = 0; for (index = 0; index < ndisk; index++) { if (diskcopy[index] > 0) { chksum2 += ((size_t)diskcopy[index]/10) * index; } } printf("%llu\n", chksum2); 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 |
︙ | ︙ |