-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.c
330 lines (281 loc) · 6.46 KB
/
14.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MEM_SIZE 100000
#define BITN 36
#define FLOAT_COUNT 1024
typedef unsigned long ulong;
typedef struct
{
ulong address;
ulong value;
} memVal;
size_t iterations = 0;
ulong emulate(FILE *in);
void parseMask(ulong *mask, char *line);
int getMemAddress(char *line);
ulong getValue(char *line);
ulong memSum(ulong *mem);
ulong emulate2(FILE *in);
void store(memVal *mem, ulong address, ulong value);
void storeWithMask(memVal *mem, ulong address, ulong value, char *mask);
ulong *getMaskedValues(ulong val, char *mask);
ulong getDefiniteMask(char *mask);
ulong setBit(int off, int bit, ulong val);
int **powerSet(int *set);
int *complement(int *set, int *sub);
ulong memSum2(memVal *mem);
int main(int argc, char *argv[])
{
FILE *in = fopen("in14", "r");
ulong part1 = emulate(in);
printf("Part 1: %lu\n", part1);
rewind(in);
ulong part2 = emulate2(in);
printf("Part 2: %lu\n", part2);
printf("Iterations: %lu\n", iterations);
fclose(in);
return 0;
}
ulong emulate2(FILE *in)
{
char *mask = malloc(BITN * sizeof(*mask));
memVal *mem = calloc(MEM_SIZE, sizeof(*mem));
char *line = NULL;
size_t n = 0;
while (!(getline(&line, &n, in) < 0 || line[0] == '\0' || line[0] == '\n'))
{
iterations++;
*strchr(line, '\n') = '\0';
if (strstr(line, "mask") != NULL)
{
memcpy(mask, strchr(line, '=')+2, BITN);
continue;
}
//printf("Mask: %s\n", mask);
int address = getMemAddress(line);
//printf("Got address: %d\n", address);
ulong value = getValue(line);
//printf("Got value: %lu\n", value);
storeWithMask(mem, address, value, mask);
}
ulong sum = memSum2(mem);
free(line);
free(mem);
free(mask);
return sum;
}
void storeWithMask(memVal *mem, ulong address, ulong value, char *mask)
{
ulong *maskValues = getMaskedValues(address, mask);
//printf("Got masked values\n");
for (int i = 0; maskValues[i] != 0; i++)
{
iterations++;
//printf("Storing %lu at %lu\n", value, maskValues[i]);
store(mem, maskValues[i], value);
}
free(maskValues);
}
ulong setBit(int off, int bit, ulong val)
{
return bit ? val | (1l << off) : val & (~(1l << off));
}
ulong *getMaskedValues(ulong val, char *mask)
{
ulong definite = getDefiniteMask(mask);
int *floating = calloc(BITN, sizeof(*floating));
ulong *res = calloc(FLOAT_COUNT, sizeof(*res));
int fi = 0;
val |= definite;
for (int i = BITN-1; i >= 0; i--)
{
iterations++;
if (mask[i] == 'X')
floating[fi++] = BITN-i;
}
//printf("Got some floatings\n");
int **floatingPossibilities = powerSet(floating);
//printf("Got a power set\n");
for (int i = 0; floatingPossibilities[i] != NULL; i++)
{
int *set = floatingPossibilities[i];
int *clear = complement(floating, set);
ulong curr = val;
for (int j = 0; set[j] != 0; j++)
{
iterations++;
//printf("\t\tSetting %d\n", set[j]-1);
curr = setBit(set[j]-1, 1, curr);
}
for (int j = 0; clear[j] != 0; j++)
{
iterations++;
//printf("\t\tClearing %d\n", clear[j]-1);
curr = setBit(clear[j]-1, 0, curr);
}
res[i] = curr;
//printf("\tGot masked value %lu\n", res[i]);
free(set);
free(clear);
}
free(floatingPossibilities);
free(floating);
return res;
}
int **powerSet(int *set)
{
int **res = calloc(FLOAT_COUNT, sizeof(*res));
res[0] = calloc(BITN, sizeof(*res[0]));
if (*set == 0)
// Empty set
return res;
int **sub = powerSet(set+1);
for (int i = 0, j=0; sub[j] != NULL; j++)
{
iterations++;
// Without the first element
res[i++] = sub[j];
// With the first element
res[i] = calloc(BITN, sizeof(*res[i]));
res[i][0] = set[0];
memcpy(res[i++]+1, sub[j], BITN-1);
}
free(sub);
return res;
}
int *complement(int *set, int *sub)
{
int *res = calloc(BITN, sizeof(*res));
int i = 0, k = 0;
loop:
for (; set[i] != 0; i++)
{
for (int j = 0; sub[j] != 0; j++)
{
iterations++;
// Sub contains element
if (sub[j] == set[i])
{
i++;
goto loop;
}
}
// Sub doesn't contain element
res[k++] = set[i];
}
return res;
}
void store(memVal *mem, ulong address, ulong value)
{
for (int i = 0; i < MEM_SIZE; i++)
{
iterations++;
if (mem[i].address == 0)
{
//printf("Found free memory at %d\n", i);
mem[i].address = address;
mem[i].value = value;
return;
}
if (mem[i].address == address)
{
//printf("Address %d was already known\n", address);
mem[i].value = value;
return;
}
}
fprintf(stderr, "Too small memory\n");
return;
}
ulong emulate(FILE *in)
{
ulong *mask = calloc(2, sizeof(*mask));
ulong *mem = calloc(MEM_SIZE, sizeof(*mem));
char *line = NULL;
size_t n = 0;
while (!(getline(&line, &n, in) < 0 || line[0] == '\0' || line[0] == '\n'))
{
iterations++;
*strchr(line, '\n') = '\0';
if (strstr(line, "mask") != NULL)
{
parseMask(mask, line);
continue;
}
int address = getMemAddress(line);
ulong value = getValue(line);
mem[address] = value & mask[0] | mask[1];
}
ulong sum = memSum(mem);
free(line);
free(mask);
free(mem);
return sum;
}
void parseMask(ulong *mask, char *line)
{
char *token = strtok(line, "=");
if ((token = strtok(NULL, "=")) == NULL)
{
fprintf(stderr, "Invalid token for line %s\n", line);
return;
}
mask[0] = 0, mask[1] = 0;
for (; *token != '\0'; token++)
{
iterations++;
if (*token == ' ')
continue;
// And mask -> X and 1 become 1
mask[0] = (mask[0] << 1) + (*token != '0');
// Or mask -> Only 1 becomes 1
mask[1] = (mask[1] << 1) + (*token == '1');
}
}
ulong getDefiniteMask(char *mask)
{
ulong res = 0;
for (; *mask != '\0'; mask++)
{
iterations++;
if (*mask == ' ')
continue;
res = (res << 1) + (*mask == '1');
}
return res;
}
int getMemAddress(char *line)
{
char *start = strchr(line, '[') + 1;
char *end = strchr(line, ']');
*end = '\0';
int res = atoi(start);
*end = ']';
return res;
}
ulong getValue(char *line)
{
return atol(strchr(line, '=') + 2);
}
ulong memSum(ulong *mem)
{
ulong sum = 0;
for (int i = 0; i < MEM_SIZE; i++)
{
iterations++;
sum += mem[i];
}
return sum;
}
ulong memSum2(memVal *mem)
{
ulong sum = 0;
for (int i = 0; mem[i].address != 0; i++)
{
iterations++;
//printf("Summing %lu\n", mem[i].value);
sum += mem[i].value;
}
return sum;
}