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
|
#include "../../lib/uthash.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct mem {
long address;
long value;
UT_hash_handle hh;
};
void new_entry(struct mem **mem, long address, long value)
{
struct mem *addr;
HASH_FIND(hh, *mem, &address, sizeof(address), addr);
if (addr == NULL) {
addr = malloc(sizeof(*addr));
addr->address = address;
HASH_ADD(hh, *mem, address, sizeof(address), addr);
}
addr->value = value;
}
long part_one(FILE *fp)
{
char *line = NULL;
size_t len = 0;
char mask[36] = { 0 };
struct mem *mem = NULL;
while (getline(&line, &len, fp) != -1) {
if (line[0] == 'm' && line[1] == 'a') {
sscanf(line, "mask = %s\n", &mask[0]);
} else if (line[0] == 'm' && line[1] == 'e') {
int address;
long value;
sscanf(line, "mem[%d] = %lu\n", &address, &value);
for (char *p = mask; *p; p++) {
long shift = 1ul << (35 - (p - mask));
if (*p == '0')
value &= ~shift;
else if (*p == '1')
value |= shift;
}
new_entry(&mem, address, value);
}
}
long sum = 0;
struct mem *addr, *tmp;
HASH_ITER(hh, mem, addr, tmp)
{
sum += addr->value;
HASH_DEL(mem, addr);
free(addr);
}
return sum;
}
long part_two(FILE *fp)
{
char *line = NULL;
size_t len = 0;
char mask[36] = { 0 };
int floating_bits[64];
struct mem *mem = NULL;
while (getline(&line, &len, fp) != -1) {
if (line[0] == 'm' && line[1] == 'a') {
sscanf(line, "mask = %s\n", &mask[0]);
} else if (line[0] == 'm' && line[1] == 'e') {
long address = 0, value = 0;
sscanf(line, "mem[%lu] = %lu\n", &address, &value);
int floating = 0;
for (int i = 0; i < 36; i++) {
if (mask[i] == '1') {
address |= 1ul << (36 - i - 1);
} else if (mask[i] == 'X') {
floating_bits[floating] = i;
floating++;
}
}
long masked_addr = address;
for (long perm = 0; perm < (1 << floating); ++perm) {
long address = masked_addr;
for (int j = 0; j < floating; j++)
if (perm & (1ul << j))
address ^= (1ul << (36 - floating_bits[j] - 1));
new_entry(&mem, address, value);
}
}
}
long sum = 0;
struct mem *addr, *tmp;
HASH_ITER(hh, mem, addr, tmp)
{
sum += addr->value;
HASH_DEL(mem, addr);
free(addr);
}
return sum;
}
int main(int argc, char *argv[])
{
FILE *fp = fopen("input", "r");
if (!fp)
exit(EXIT_FAILURE);
clock_t tic = clock();
printf("%lu\n", part_one(fp));
rewind(fp);
printf("%lu\n", part_two(fp));
clock_t toc = clock();
printf("TIME: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
fclose(fp);
return 0;
}
|