-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhunt.c
More file actions
355 lines (314 loc) · 9.31 KB
/
hunt.c
File metadata and controls
355 lines (314 loc) · 9.31 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "hunt.h"
#include <stdio.h>
/* Task 1 - Axii */
uint16_t find_spell(uint64_t memory)
{
/*
* The spell is 16 bits and they are in the memory exactly after 5
* consecutive bits of 1:
*
* high low
* [][][x]...[x][x][x][1][1][1][1][1][]...[][][]
* |----16bits---|
* Spell
*
* There is only 1 group of 5 consecutive bits of 1 and the other groups
* have less than consecutive bits of 1
* Steps:
* - find 5 consecutive bits of 1
* - the NEXT 16 bits == spell
*/
uint16_t res = 0;
int i,j,k,l,m,n,spel,r1;
for (i=0; i<=63; i++)
{
uint64_t mask = ((uint64_t)1 << i);
if((memory & mask) != 0)
{
spel=1;
for (j = 1; j < 5; j++)
{
k=i+j ;
uint64_t mask_1 = ((uint64_t)1<<k) ;
if ((memory & mask_1 ) != 0)
spel=spel+1;
else
break;
}
if (spel == 5 )
{
m=i+5;
res=0;
for (l=0; l< 16;l++)
{
n=m+l;
uint64_t mask_2 = ((uint64_t)1 << n);
if((memory & mask_2) != 0)
r1 = 1;
else
r1 = 0;
if (r1 == 0)
{
uint16_t mask11 = ~((uint16_t)1 << l);
res = res & mask11 ;
}
else
{
uint16_t mask12 = ((uint16_t)1 << l);
res = res | mask12 ;
}
}
}
}
}
/* TODO */
return res;
}
uint16_t find_key(uint64_t memory)
{
/*
* The key is 16 bits and they are in the memory exactly before 3
* consecutive bits of 1:
*
* high low
* [][][][1][1][1][x][x]...[x][x][]...[][][]
* |----16bits---|
* Key
* Steps:
* - find 3 consecutive bits of 1
* - the PREVIOUS 16 bits == key
*/
uint16_t res = 0;
int i,j,k,l,m,n,spel,r1;
for (i=63; i >= 0; i--)
{
uint64_t mask = ((uint64_t)1 << i);
if((memory & mask) != 0)
{
spel=1;
for (j = 1; j < 3; j++)
{
k=i-j ;
uint64_t mask_1 = ((uint64_t)1<<k) ;
if ((memory & mask_1 ) != 0)
spel=spel+1;
else
break;
}
if (spel == 3 )
{
m=i-3;
n=m+1;
res=0;
for (l=15; l >= 0; l--)
{
n=n-1;
uint64_t mask_2 = ((uint64_t)1 << n);
if((memory & mask_2) != 0)
r1 = 1;
else
r1 = 0;
if (r1 == 0)
{
uint16_t mask11 = ~((uint16_t)1 << l);
res = res & mask11 ;
}
else
{
uint16_t mask12 = ((uint16_t)1 << l);
res = res | mask12 ;
}
}
}
}
}
/* TODO */
return res;
}
uint16_t decrypt_spell(uint16_t spell, uint16_t key)
{
/*
* Find the spell knowing that
* spell_encrypted = spell_plaintext ^ key
*/
uint16_t res = -1;
res=spell^key;
/* TODO */
return res;
}
/* Task 2 - Sword Play */
uint32_t choose_sword(uint16_t enemy)
{
/*
* Help Geralt construct a sword that will give him an advantage in battle.
* The steps we will follow:
* - choose the corect sword
* - brand the sword with the correct runes such that the equation
* for that specific enemy type is true
*
* How does the sword look (Sword template):
* [][][][] [0][0][0]...[0][0][0] [][][][][][][][][][][][][][][][]
* -------- --------------------- --------------------------------
* Type Does not matter The "Value" such that the equation
* from 2 holds depending on the enemy type
* 4 bits 12 bits 16 bits
*
* 1. Type of the sword:
* - 1001 - Silver Sword - for monsters
* - 0110 - Steel Sword - for humans
*
* If the number of active bits from variable "enemy" is even == Monster
* else == Human
*
* 2.
* Monster Equation:
* Value ^ (Enemy & (1 - Enemy)) = 0
*
* Human Equation:
* Value + Enemy = 0
*/
uint32_t res = -1, mask;
uint8_t i, nr = 0;
uint16_t value;
for (i=0; i<15; i++)
{
mask = 1 << i;
if((enemy & mask) != 0)
nr++;
}
if (!(nr % 2))
{
value = enemy & (1- enemy);
res = (9ULL << 28) + value;
}
else
{ value= - enemy;
res = (6ULL << 28) +value;
}
return res;
}
/* Task 3 - The Witcher Trials */
uint32_t trial_of_the_grasses(uint16_t cocktail)
{
/*
* To become a witcher one must survive a cocktail of different
* herbs. The body should generate the correct antibodies to
* neutralize the given cocktail.
*
*
* The antibodies need to respect the following properties:
* (antibodies_high & cocktail) ^ (antibodies_low | cocktail) = 0
* antibodies_low & antibodies_high = 0
* antibodies_low | antibodies_high = cocktail
*
* Where:
* [][][]...[][][] | [][][]...[][][]
* --------------- ---------------
* antibodies_high antibodies_low
* 16 bits 16 bits
* -------------------------
* antibodies
*/
uint32_t res = -1;
res = cocktail << 16;
return res;
/* TODO */
return res;
}
uint8_t trial_of_forrest_eyes(uint64_t map)
{
/*
* For the next trail, the candidate is tied up and blindfolded.
* They are also taken to a forest and they should return until
* the next morning.
*
* The candidate knows that there are 4 possibilities for the forest:
* Brokilon, Hindar, Wolven Glade and Caed Dhu.
*
* The candidate also knows those forests by heart, but first you
* need to identify the forest.
*
* The characteristics for 3 of the forests are as follows:
* - Brokilon - has ONLY groups of 4 trees (or a forest without any tree)
* Ex: a group of 4 trees: "...00111100..."
* - Hindar - has ONLY 2 trees in the MIDDLE
* - Wolven Glade - FULL of trees
*
* The "map" variable encodes the following information:
* - 1 tree
* - 0 patch of grass
*
* You should put in the "res" variable the index for the identified
* forrest.
* - Brokilon - index 0
* - Hindar - index 1
* - Wolven Glade - index 2
* - Caed Dhu - index 3
*/
uint8_t res = -1, k=0, i;
uint64_t mask;
for(i=0; i<64; i++)
{
mask = 1ULL <<i;
if((mask & map) != 0)
k++;
}
switch(k)
{
case 0 :
res = 0;
break;
case 2 :
res = 1;
break;
case 64 :
res = 2;
break;
default :
if(!(k % 4))
res = 0;
else
res = 3 ;
}
/* TODO */
return res;
}
uint8_t trial_of_the_dreams(uint32_t map)
{
/*
* For this final trial, the candidates go into a trance and when
* they woke up they would be a fully fledged witcher.
*
* For this task, you need to help the witcher find the number
* of steps that would take the candidate from the dream realm to
* the real world.
*
* The "map" variable would have only 2 bits of 1:
* - candidate location
* - portal location
*
* You should put in the "res" variable the distance from the
* candidate position to the portal.
*
* Ex:
* [0][0][0][0][0][0]....[1][0][0][0][0][1][0]....[0][0][0]
*
* res = 5
*/
/* TODO */
uint8_t i, c1, c2, r = 0;
uint32_t mask;
for(i = 0; i < 32; i++)
{
mask = 1 << i;
if((mask & map) != 0)
{ if (!r)
{ c1 = i;
r = 1;
}
else
c2 = i;
}
}
return c2 - c1;
}