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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define arrlen(x) (sizeof(x)/sizeof((x)[0]))
struct rock {
unsigned char s[4];
};
struct rock const rocks[] = {
{ { 0x1e, 0, 0, 0 } }, // Horizontal line
{ { 0x08, 0x1c, 0x08, 0 } }, // Cross
{ { 0x1c, 0x04, 0x04, 0 } }, // Elbow
{ { 0x10, 0x10, 0x10, 0x10 } }, // Line piece
{ { 0x18, 0x18, 0, 0 } }, // Block
};
struct cheat {
unsigned long depth;
struct rock rock;
unsigned long num;
unsigned long pos;
};
unsigned long
cheat_numdiff(struct cheat * cheats, int ncheats)
{
return cheats[0].num - cheats[1].num;
}
unsigned long
cheat_posdiff(struct cheat * cheats, int ncheats)
{
return cheats[0].pos - cheats[1].pos;
}
int
cheat_check(struct cheat * cheats, int ncheats,
unsigned long depth, struct rock const * rock,
unsigned long rocknum, unsigned long position)
{
unsigned long numdiff, posdiff;
int i;
if (depth < cheats[0].depth)
// Not deep enough.
return 0;
if (depth == cheats[0].depth
&& memcmp(rock, &cheats[0].rock, sizeof(struct rock)) != 0)
// Same depth as previously, but differnt rock, or in different
// position in chamber.
return 0;
// Rock is either deeper than any previous, or is as deep and is the
// same rock in the same position in the chamber
fprintf(stderr, "Deepest rock %lu (depth %lu, height %lu)\n",
rocknum, depth, position);
memmove(cheats + 1, cheats, sizeof(struct cheat) * (ncheats - 1));
cheats[0].depth = depth;
cheats[0].rock = *rock;
cheats[0].num = rocknum;
cheats[0].pos = position;
numdiff = cheat_numdiff(cheats, ncheats);
posdiff = cheat_posdiff(cheats, ncheats);
for (i = 2; i < ncheats; ++i)
if (cheats[i - 1].num - cheats[i].num != numdiff
|| cheats[i - 1].pos - cheats[i].pos != posdiff)
// Not all the cheats are the same
return 0;
// All the cheats are the same!
return 1;
}
#if 0
void
chamber_dump(unsigned char * chamber, int chamberheight, struct rock * rock, int rockheight)
{
int i;
for (i = chamberheight - 1; i >= 0; --i) {
unsigned char c;
fputc('|', stderr);
for (c = 0x40; c != 0; c >>= 1) {
if (rock && i >= rockheight
&& i < rockheight + arrlen(rock->s)
&& (rock->s[i - rockheight] & c))
fputc('@', stderr);
else if (chamber && (chamber[i] & c))
fputc('#', stderr);
else
fputc('.', stderr);
}
fputc('|', stderr);
fputc('\n', stderr);
}
fprintf(stderr, "+-------+\n");
}
#endif
int
main(int argc, char ** argv)
{
int docheat = 0;
size_t jetsize = 0;
char * jets = NULL;
unsigned char * chamber = NULL;
unsigned long chamberheight = 0, top = 0, cheatby = 0;
unsigned long movement, rocknum, rockheight;
unsigned long rocklimit = 2022;
struct cheat cheats[10] = {0};
struct rock rock;
int i;
while ((i = getopt(argc, argv, "p:r:c")) != -1) {
switch (i) {
case 'p':
switch (atoi(optarg)) {
case 1: rocklimit = 2022; break;
case 2: rocklimit = 1000000000000; break;
default:
fprintf(stderr, "Bad part: %s\n", optarg);
return -1;
}
break;
case 'r':
rocklimit = atol(optarg);
break;
case 'c':
docheat = 1;
break;
default:
return -1;
}
}
// Read jets.
while (1) {
void * p;
size_t s;
if ((p = realloc(jets, jetsize + BUFSIZ)) == NULL) {
fprintf(stderr, "jets: bad realloc (%zu)\n", jetsize + BUFSIZ);
free(jets);
return -1;
}
jets = p;
if ((s = fread(jets + jetsize, 1, BUFSIZ, stdin)) == 0)
break;
jetsize += s;
}
// Remove any characters that are not '<' or '>' (e.g. newlines)
for (i = 0; i < jetsize; ++i) {
while (i < jetsize && jets[i] != '<' && jets[i] != '>') {
memmove(jets + i, jets + i + 1, jetsize - (i + 1));
--jetsize;
}
}
// Place first rock, and start movement
rocknum = 0;
rock = rocks[rocknum];
rockheight = 3;
for (movement = 0; rocknum < rocklimit; ++movement) {
int jet;
if (!chamberheight || top > chamberheight - 10) {
void * p;
if ((p = realloc(chamber, chamberheight + BUFSIZ)) == NULL) {
fprintf(stderr, "Bad realloc(%lu)\n", chamberheight + BUFSIZ);
free(chamber);
free(jets);
return -1;
}
chamber = p;
memset(chamber + chamberheight, 0, BUFSIZ);
chamberheight += BUFSIZ;
}
jet = jets[movement % jetsize];
// Check if jet can push rock.
for (i = 0; i < arrlen(rock.s); ++i) {
// Check left wall collision
if (jet == '<' && (rock.s[i] & 0x40) != 0)
jet = 0;
// Check right wall collision
if (jet == '>' && (rock.s[i] & 0x01) != 0)
jet = 0;
// Check moving left into existing rocks.
if (jet == '<' && ((rock.s[i] << 1) & chamber[rockheight + i]) != 0)
jet = 0;
// Check moving right into existing rocks
if (jet == '>' && ((rock.s[i] >> 1) & chamber[rockheight + i]) != 0)
jet = 0;
}
// If jet can push rock, do so.
if (jet == '<')
for (i = 0; jet && i < arrlen(rock.s); ++i)
rock.s[i] <<= 1;
if (jet == '>')
for (i = 0; jet && i < arrlen(rock.s); ++i)
rock.s[i] >>= 1;
// Check if rock can fall.
for (i = 0; rockheight > 0 && i < arrlen(rock.s); ++i)
if ((rock.s[i] & chamber[rockheight - 1 + i]) != 0)
break;
if (i < arrlen(rock.s)) {
// Rock cannot fall. Place rock.
for (i = 0; i < arrlen(rock.s) && rock.s[i] != 0; ++i) {
chamber[rockheight + i] |= rock.s[i];
if (rockheight + i > top)
top = rockheight + i;
}
// Check to see if we can cheat
if (docheat
&& rocknum > arrlen(rocks) * jetsize
&& rockheight < top
&& cheat_check(cheats, arrlen(cheats),
top - rockheight, &rock,
rocknum, rockheight))
{
unsigned long numdiff, posdiff, steps;
numdiff = cheat_numdiff(cheats, arrlen(cheats));
posdiff = cheat_posdiff(cheats, arrlen(cheats));
steps = (rocklimit - rocknum) / numdiff;
fprintf(stderr, "Cheating from rock %lu to %lu in %lu steps of %lu rocks/%lu height\n",
rocknum, rocklimit, steps, numdiff, posdiff);
rocknum += steps * numdiff;
cheatby += steps * posdiff;
}
// Pick next rock.
rock = rocks[++rocknum % arrlen(rocks)];
rockheight = top + 4;
#if 0
chamber_dump(chamber, top + 7, &rock, rockheight);
#endif
}
else {
// Rock can fall, so reduce its height by 1
--rockheight;
}
}
printf("Height of rocks in chamber: %lu\n", top + cheatby + 1);
free(chamber);
free(jets);
return 0;
}
|