-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.c3
278 lines (258 loc) · 5.6 KB
/
day17.c3
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
module day17;
import std::io;
import std::math;
import std::collections::map;
import std::collections::list;
def StateCacheMap = HashMap(<StateCache, int>);
struct StateCache
{
int shape_index;
int wind_index;
GameRow[128] rows;
int height;
}
fn uint StateCache.hash(StateCache* cache)
{
uint hash = cache.wind_index * 5 + cache.shape_index;
return hash * 31 + cache.height;
}
macro bool StateCache.equals(StateCache* cache, StateCache other)
{
if (cache.shape_index != other.shape_index) return false;
if (cache.wind_index != other.wind_index) return false;
if (cache.height != other.height) return false;
for (int i = 0; i < cache.height; i++)
{
if (cache.rows[i] != other.rows[i]) return false;
}
return true;
}
fn String load_jets()
{
File f = file::open("jets.txt", "rb")!!;
defer (void)f.close();
return io::readline(&f)!!;
}
struct Shape
{
int width;
int height;
int[4][4] shape;
}
Shape[5] shapes = {
{
4, 1,
{ { 1, 1, 1, 1 }, {}, {}, {} }
},
{
3, 3,
{
{ 0, 1, 0, 0 },
{ 1, 1, 1, 0 },
{ 0, 1, 0, 0 }, {}
}
},
{
3, 3,
{
{ 1, 1, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 },
{} }
},
{
1, 4,
{
{ 1, 0, 0, 0 },
{ 1, 0, 0, 0 },
{ 1, 0, 0, 0 },
{ 1, 0, 0, 0 }
}
},
{
2, 2,
{
{ 1, 1, 0, 0 },
{ 1, 1, 0, 0 }, {}, {}
}
}
};
distinct GameRow = char;
GameRow[1000] game;
macro bool GameRow.is_full(GameRow row)
{
return row == 127;
}
macro bool GameRow.bit(GameRow row, int bit)
{
return ((GameRow)(1 << bit) & row) != 0;
}
macro bool GameRow.set_bit(GameRow* row, int bit)
{
return (bool)(*row |= (GameRow)1 << bit);
}
fn int find_height()
{
int size = game.len;
for (int i = 0; i < size; i++)
{
if (game[i] == 0) return i;
}
unreachable("Max exceeded");
}
fn int find_lowest_bound(int height)
{
GameRow ok;
for (int i = height; i >= 0; i--)
{
ok |= game[i];
if (ok.is_full()) return i;
}
return 0;
}
fn bool check_collision(Shape* shape, int[<2>] location)
{
if (location[1] < 0) return true;
if (location[0] < 0) return true;
if (location[0] + shape.width > 7) return true;
int y_offset = shape.height;
for (int y = 0; y < shape.height; y++)
{
for (int x = 0; x < shape.width; x++)
{
if (!shape.shape[y][x]) continue;
if (game[location[1] + y].bit(location[0] + x)) return true;
}
}
return false;
}
fn void land_shape(Shape* shape, int[<2>] location)
{
for (int y = 0; y < shape.height; y++)
{
for (int x = 0; x < shape.width; x++)
{
if (!shape.shape[y][x]) continue;
game[location[1] + y].set_bit(location[0] + x);
}
}
}
fn void draw_field()
{
io::printn("+-----+");
for (int i = find_height(); i >= 0; i--)
{
GameRow row = game[i];
for (int j = 0; j < 7; j++)
{
io::putchar(row.bit(j) ? '#' : '.');
}
io::printn();
}
io::printn("+-----+");
}
macro long adjust_lowest_bounds()
{
int height = find_height();
int bound = find_lowest_bound(height);
if (bound == 0) return 0;
for (int i = bound; i < height; i++)
{
game[i - bound] = game[i];
}
for (int i = height - bound; i < height; i++)
{
game[i] = 0;
}
return bound;
}
macro @drop_rock(String winds, int* &shape_index, isz* &wind_index)
{
int start = find_height() + 3;
Shape* current_shape = &shapes[*shape_index];
*shape_index = (*shape_index + 1) % 5;
int[<2>] location = { 2, start };
while (true)
{
char jet = winds[*wind_index];
*wind_index = (*wind_index + 1) % winds.len;
int[<2>] new_loc = location + { jet == '<' ? -1 : 1, 0 };
if (check_collision(current_shape, new_loc))
{
new_loc = location;
}
location = new_loc;
new_loc[1]--;
if (check_collision(current_shape, new_loc))
{
land_shape(current_shape, location);
break;
}
location = new_loc;
}
}
fn void solve(String winds, long rocks)
{
game = {};
int shape_index = 0;
isz wind_index = 0;
long offset = 0;
StateCacheMap map;
map.new_init(1024);
long max = rocks;
bool check_cache = true;
long desired_drops = rocks;
long first_cycle_hit = -1;
long cycle_start_height = 0;
long cycle_start_rock = 0;
for (long i = 0; i < max; i++)
{
if (check_cache)
{
int height = find_height();
int bottom = find_lowest_bound(height);
StateCache cache = { .shape_index = shape_index, .wind_index = (int)wind_index, .height = height - bottom - 1 };
assert(height - bottom < (int)cache.rows.len);
for (int k = bottom; k < height; k++)
{
cache.rows[k - bottom] = game[k];
}
int! index = map.get(cache);
if (try index)
{
{|
if (first_cycle_hit < 0)
{
first_cycle_hit = index;
cycle_start_rock = i;
cycle_start_height = find_height() + offset;
return;
}
if (index != first_cycle_hit) return;
long cycle_height = find_height() + offset - cycle_start_height;
long cycle_len = (long)i - cycle_start_rock;
desired_drops -= i;
long cycles = desired_drops / cycle_len;
offset += cycles * cycle_height;
max = desired_drops % cycle_len;
i = 0;
check_cache = false;
|};
}
else
{
map.set(cache, (int)i);
}
}
@drop_rock(winds, shape_index, wind_index);
offset += adjust_lowest_bounds();
}
io::printfn("Height was: %d", find_height() + offset);
}
fn void main()
{
String s = load_jets();
defer s.free();
solve(s, 2022);
solve(s, 1000000000000i64);
}