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 |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <math.h>
struct row
{
unsigned long n[12];
unsigned long target;
int len;
};
static unsigned long sum(struct row *r, unsigned int mask)
{
unsigned long tot = 0;
int i;
tot = r->n[0];
for(i = 1; i < r->len; i++)
{
if(mask & 1)
tot *= r->n[i];
else
tot += r->n[i];
mask >>= 1;
}
return tot;
}
static int dop1(struct row *r)
{
unsigned int mask;
unsigned int siz;
siz = (1 << r->len)-1;
for(mask = 0; mask < siz; mask++)
{
if(sum(r, mask) == r->target)
return 1;
}
return 0;
}
static int p2_mul(struct row *r, int depth, unsigned long sum);
static int p2_or(struct row *r, int depth, unsigned long sum);
static int p2_add(struct row *r, int depth, unsigned long sum)
{
if(depth >= r->len)
return 0;
sum += r->n[depth];
if(sum == r->target && depth == r->len-1)
return 1;
if(sum > r->target)
return 0;
if(p2_add(r, depth+1, sum))
return 1;
if(p2_or(r, depth+1, sum))
return 1;
if(p2_mul(r, depth+1, sum))
return 1;
return 0;
}
static int p2_or(struct row *r, int depth, unsigned long sum)
{
if(depth >= r->len)
return 0;
if(r->n[depth] < 10)
sum = sum * 10 + r->n[depth];
else if(r->n[depth] < 100)
sum = sum * 100 + r->n[depth];
else
sum = sum * 1000 + r->n[depth];
if(sum == r->target && depth == r->len-1)
return 1;
if(sum > r->target)
return 0;
if(p2_add(r, depth+1, sum))
return 1;
if(p2_or(r, depth+1, sum))
return 1;
if(p2_mul(r, depth+1, sum))
return 1;
return 0;
}
static int p2_mul(struct row *r, int depth, unsigned long sum)
{
if(depth >= r->len)
return 0;
sum *= r->n[depth];
if(sum == r->target && depth == r->len-1)
return 1;
if(sum > r->target)
return 0;
if(p2_add(r, depth+1, sum))
return 1;
if(p2_or(r, depth+1, sum))
return 1;
if(p2_mul(r, depth+1, sum))
return 1;
return 0;
}
static int dop2a(struct row *r)
{
unsigned long sum = 0;
sum = r->n[0];
if(p2_add(r, 1, sum))
return 1;
if(p2_or(r, 1, sum))
return 1;
if(p2_mul(r, 1, sum))
return 1;
return 0;
}
static unsigned long sum2(struct row *r, unsigned int mask)
{
unsigned long tot = 0;
int i;
tot = r->n[0];
for(i = 1; i < r->len; i++)
{
if(mask % 3 == 0)
tot *= r->n[i];
else if(mask % 3 == 1)
tot += r->n[i];
else
{
if(r->n[i] < 10)
tot = tot*10 + r->n[i];
else if(r->n[i] < 100)
tot = tot*100 + r->n[i];
else
tot = tot*1000 + r->n[i];
}
if(tot > r->target)
return 0;
mask /= 3;
}
return tot;
}
static int dop2(struct row *r)
{
unsigned int siz, mask;
siz = powl(3, r->len-1);
for(mask = 0; mask < siz; mask++)
{
if(sum2(r, mask) == r->target)
return 1;
}
return 0;
}
static void timedump(struct timeval *t1, struct timeval *t2)
{
unsigned long sec = t2->tv_sec - t1->tv_sec;
unsigned long usec = t2->tv_usec - t1->tv_usec;
if(sec)
{
printf("%lu.%lus", sec, usec/1000);
return;
}
if(usec > 1000)
{
printf("%lums", usec/1000);
return;
}
printf("%luµs", usec);
}
int main(void)
{
FILE *f;
struct timeval t1, t2;
unsigned long long p1 = 0, p2 = 0;
f = fopen("day7.txt", "rb");
if(!f)
return 1;
gettimeofday(&t1, NULL);
while(1)
{
char buf[1024];
struct row r = {0};
char *p = buf;
int i;
if(!fgets(buf, 1024, f))
break;
if(buf[0] == '\n')
break;
r.target = strtol(buf, &p, 10);
p++;
i = 0;
while(1)
{
r.n[i] = strtol(p, &p, 10);
i++;
if(*p == ' ')
p++;
if(!*p || *p == '\n')
break;
}
r.len = i;
if(dop1(&r))
p1 += r.target;
if(dop2a(&r))
p2 += r.target;
}
gettimeofday(&t2, NULL);
printf("p1: %llu, p2: %llu in ", p1, p2);
timedump(&t1, &t2);
putchar('\n');
return 0;
} |