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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#define INPUTLEN 20000
static int* expand(size_t *lenp, const char *mp, size_t maplen) {
size_t len = 0;
for (size_t i = 0; i < maplen; i++) len += mp[i] - '0';
int *disk = malloc(len * sizeof(int));
size_t cursor = 0;
for (size_t i = 0; i + 1 < maplen; i += 2) {
for (int j = 0; j < mp[i] - '0'; j++) disk[cursor++] = i / 2;
for (int j = 0; j < mp[i+1] - '0'; j++) disk[cursor++] = -1;
}
if (maplen % 2 == 1) {
size_t i = maplen - 1;
for (int j = 0; j < mp[i] - '0'; j++) disk[cursor++] = i / 2;
}
*lenp = len;
return disk;
}
__attribute__((unused))
static void print_disk(int *disk, size_t len) {
for (size_t i = 0; i < len; i++) {
if (i != 0) putchar(' ');
if (disk[i] == -1) putchar('.');
else printf("%d", disk[i]);
}
putchar('\n');
}
static size_t compact(int *disk, size_t len) {
size_t j = len; // one past the last non-empty block
while (j > 0 && disk[j-1] == -1) j--;
for (size_t i = 0; i < j - 1; i++) {
if (disk[i] != -1) continue;
disk[i] = disk[--j];
while (j > 0 && disk[j-1] == -1) j--;
}
return j;
}
static void defragment(int *disk, const size_t len) {
printf("len = %zu\n", len);
int *blockoff = malloc(len * sizeof(int));
int *blocks = malloc(len * sizeof(int)); // negative is empty
int nblocks = 0;
for (int i = 0; i < (int)len; ) {
blockoff[nblocks] = i;
int blocklen = 1;
for (; i + blocklen < (int)len && disk[i + blocklen] == disk[i]; blocklen++) {}
blocks[nblocks++] = disk[i] == -1 ? -blocklen : blocklen;
i += blocklen;
}
// printf("blockoff:"); for (int k = 0; k < nblocks; k++) printf(" %d", blockoff[k]); printf("\n\n");
for (int j = nblocks - 1; j > 0; j--) {
if (blocks[j] < 0) continue;
// print_disk(disk, len);
// printf("blocks:"); for (int k = 0; k < nblocks; k++) printf(" %s%d%s", k==j?"[":"", blocks[k], k==j?"]":""); printf("\n\n");
int i = 0;
while (i < j && (blocks[i] >= 0 || disk[blockoff[i]] >= 0 || -blocks[i] < blocks[j])) i++;
if (i >= j) continue;
memcpy(&disk[blockoff[i]], &disk[blockoff[j]], blocks[j] * sizeof(int));
memset(&disk[blockoff[j]], -1, blocks[j] * sizeof(int));
if (-blocks[i] - blocks[j] > 0) {
blocks[i] += blocks[j];
blockoff[i] += blocks[j];
}
}
// print_disk(disk, len);
// printf("blocks:"); for (int k = 0; k < nblocks; k++) printf(" %d", blocks[k]); printf("\n");
}
static size_t checksum(int *disk, size_t len) {
size_t sum = 0;
for (size_t i = 0; i < len; i++) {
if (disk[i] != -1) sum += i * disk[i];
}
return sum;
}
int main(void) {
char *diskmap = malloc(INPUTLEN + 1);
size_t nr = fread(diskmap, 1, INPUTLEN, stdin);
assert(nr <= INPUTLEN);
while (nr > 0 && isspace(diskmap[nr-1])) nr--;
diskmap[nr] = 0;
size_t disklen;
int *disk = expand(&disklen, diskmap, nr);
int *disk2 = malloc(disklen * sizeof(int));
memcpy(disk2, disk, disklen * sizeof(int));
size_t disklen1 = compact(disk, disklen);
printf("%zu\n", checksum(disk, disklen1));
defragment(disk2, disklen);
printf("%zu\n", checksum(disk2, disklen));
}
|