finish my shit on medium

This commit is contained in:
Maoake Teriierooiterai
2026-01-16 10:03:33 +01:00
parent afa8785379
commit 587ea1750c
9 changed files with 261 additions and 799 deletions

View File

@@ -40,7 +40,7 @@ CHECKER_FILES = check_error.c verif_flag.c verif_is_digit.c verif_overflow.c ver
STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.c
MEDIUM_ALGO = utils_medium.c utils_struct_tab.c utils_medium_two.c sort_utils.c sort_utils_two.c medium_algo.c \
sort_utils_tree.c
move_patha.c move_pathb.c
COMPLEX_ALGO = radix.c

View File

@@ -13,65 +13,6 @@
#include "push_swap.h"
#include "medium_headers.h"
#include <stdlib.h>
#include <stdio.h>
static int path_to_end(t_stacks *stacks, int max_range, int range, char c)
{
t_stack *tmp;
t_stack *start;
int i;
int first_pass;
tmp = assign_stack(stacks, c);
start = assign_stack(stacks, c);
tmp = tmp->previous;
i = 0;
first_pass = 1;
while (first_pass || tmp != start->previous)
{
first_pass = 0;
if (in_range(tmp->value, max_range, range))
tmp = start;
tmp = tmp->previous;
i++;
}
return (i);
}
static int path_to_start(t_stacks *stacks, int max_range, int range, char c)
{
t_stack *tmp;
t_stack *start;
int i;
int first_pass;
tmp = assign_stack(stacks, c);
start = assign_stack(stacks, c);
i = 0;
first_pass = 1;
while (first_pass || tmp != start)
{
first_pass = 0;
if (in_range(tmp->value, max_range, range))
{
tmp = start->previous;
}
tmp = tmp->next;
i++;
}
return (i);
}
int wich_path(t_stacks *stacks, int max_range, int range, char c)
{
int path_start;
int path_end;
path_start = path_to_start(stacks, max_range, range, c);
path_end = path_to_end(stacks, max_range, range, c);
if (path_start < path_end)
return (1);
return (0);
}
int stack_len(t_stack *stack)
{
@@ -95,7 +36,6 @@ void bucket_algo(t_stacks *stacks, t_tab *preset, int range)
tmp = preset;
while(stacks->a)
pb(stacks);
//stacks->print = 0;
while (preset)
{
push_range_to_b(stacks, preset, range);

View File

@@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move_patha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/16 06:20:47 by mteriier #+# #+# */
/* Updated: 2026/01/16 06:20:55 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "medium_headers.h"
#include "push_swap.h"
#include <stdio.h>
static int is_border_a(int value, t_stacks *stacks)
{
t_stack *tmp;
if (!stacks->a)
return (1);
tmp = assign_stack(stacks, 'a');
if ((value < tmp->value && check_order(tmp))
|| (value > tmp->value && value > tmp->previous->value
&& check_order(stacks->a)))
return (1);
return (0);
}
int move_left_to_right(int value, t_stacks *stacks, int is_move)
{
int i;
t_stack *tmp;
i = 0;
tmp = assign_stack(stacks, 'a');
while (value > tmp->value && !is_move)
{
tmp = tmp->next;
i++;
}
while (value > stacks->a->value && is_move)
ra(stacks);
return (i);
}
int move_right_to_left(int value, t_stacks *stacks, int is_move)
{
int i;
t_stack *tmp;
tmp = assign_stack(stacks, 'a');
i = 0;
while (value < tmp->previous->value && !is_move)
{
tmp = tmp->previous;
i++;
}
while (value < stacks->a->previous->value && is_move)
rra(stacks);
return (i);
}
int wich_path_a(int value, t_stacks *stacks)
{
t_stack *tmp;
int move_from_left;
int move_from_right;
tmp = assign_stack(stacks, 'a');
if (is_border_a(value, stacks))
return (-1);
move_from_left = move_left_to_right(value, stacks, 0);
move_from_right = move_right_to_left(value, stacks, 0);
if ((check_order(stacks->a) && move_from_left < move_from_right)
|| (move_from_left > move_from_right && !check_order(stacks->a)))
return (1);
else
return (0);
}
void move_ra_rra(int value, t_stacks *stacks)
{
if (is_border_a(value, stacks))
return ;
if (wich_path_a(value, stacks) == 1)
{
move_left_to_right(value, stacks, 1);
}
else if (wich_path_a(value, stacks) == 0)
{
move_right_to_left(value, stacks, 1);
}
}

View File

@@ -0,0 +1,113 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move_pathb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/16 06:20:38 by mteriier #+# #+# */
/* Updated: 2026/01/16 06:20:42 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "medium_headers.h"
#include "push_swap.h"
static int path_to_end(t_stacks *stacks, int max_range, int range, char c)
{
t_stack *tmp;
t_stack *start;
int i;
int first_pass;
tmp = assign_stack(stacks, c);
start = assign_stack(stacks, c);
tmp = tmp->previous;
i = 0;
first_pass = 1;
while (first_pass || tmp != start->previous)
{
first_pass = 0;
if (in_range(tmp->value, max_range, range))
tmp = start;
tmp = tmp->previous;
i++;
}
return (i);
}
static int path_to_start(t_stacks *stacks, int max_range, int range, char c)
{
t_stack *tmp;
t_stack *start;
int i;
int first_pass;
tmp = assign_stack(stacks, c);
start = assign_stack(stacks, c);
i = 0;
first_pass = 1;
while (first_pass || tmp != start)
{
first_pass = 0;
if (in_range(tmp->value, max_range, range))
{
tmp = start->previous;
}
tmp = tmp->next;
i++;
}
return (i);
}
int wich_path(t_stacks *stacks, int max_range, int range, char c)
{
int path_start;
int path_end;
path_start = path_to_start(stacks, max_range, range, c);
path_end = path_to_end(stacks, max_range, range, c);
if (path_start < path_end)
return (1);
return (0);
}
void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range)
{
if (wich_path(stacks, one_preset->max_range, range, 'b'))
{
while (!in_range(stacks->b->value, one_preset->max_range, range))
rb(stacks);
}
else
{
while (!in_range(stacks->b->value, one_preset->max_range, range))
{
rrb(stacks);
}
}
}
int get_pre_move_b(t_stacks *stacks, t_tab *one_preset, int range)
{
int i;
t_stack *tmp;
i = 0;
tmp = assign_stack(stacks, 'b');
if (wich_path(stacks, one_preset->max_range, range, 'b'))
{
while (!in_range(tmp->value, one_preset->max_range, range))
{
i++;
tmp = tmp->next;
}
}
else
while (!in_range(tmp->value, one_preset->max_range, range))
{
i++;
tmp = tmp->previous;
}
return (i);
}

View File

@@ -13,76 +13,58 @@
#include "push_swap.h"
#include "medium_headers.h"
#include <stdio.h>
static int move_next(t_stack *tmp, int value)
void move_rr_rrr(t_stacks *stacks, int move, char c)
{
int i;
i = 0;
if (tmp->previous->value < value)
return (70000);
while (tmp->value < value)
while (i < move && c == 's')
{
tmp = tmp->next;
rr(stacks);
i++;
}
return (i);
while (i < move && c == 'e')
{
i++;
rrr(stacks);
}
}
static int number_move_reverse(t_stacks *stacks, int value, char start_end)
void put_in_order_ra_rra(t_stacks *stacks)
{
int i;
t_stack *tmp;
int i;
i = 0;
tmp = stacks->a;
if (start_end == 's')
while (!check_order(stacks->a) && i < 50)
{
i = move_next(tmp, value);
}
else
{
tmp = tmp->previous;
while (tmp->value > value)
{
tmp = tmp->previous;
i++;
}
}
return (i);
}
int sort_path(t_stacks *stacks, int value)
{
int start_path;
int end_path;
start_path = number_move_reverse(stacks, value, 's');
if (start_path == 0)
return (1);
end_path = number_move_reverse(stacks, value, 'e');
if (start_path < end_path)
return (1);
return (0);
}
static void sort_little_pile(t_stacks *stacks)
{
if (!stacks->a)
{
pa(stacks);
return ;
}
if (stacks->a->previous->value < stacks->b->value)
{
pa(stacks);
ra(stacks);
return ;
i++;
}
if (sort_path(stacks, stacks->b->value))
sort_from_left(stacks);
else
sort_from_right(stacks);
}
void path_rr_rrr(int value, t_stacks *stacks, t_tab *one_preset, int range)
{
int move_a;
int final_move;
final_move = get_pre_move_b(stacks, one_preset, range);
if (wich_path_a(value, stacks) == 1
&& wich_path(stacks, one_preset->max_range, range, 'b'))
{
move_a = move_left_to_right(value, stacks, 0);
if (move_a < final_move)
final_move = move_a;
move_rr_rrr(stacks, final_move, 's');
}
else if (wich_path_a(value, stacks) == 0
&& !wich_path(stacks, one_preset->max_range, range, 'b'))
{
move_a = move_right_to_left(value, stacks, 0);
if (move_a < final_move)
final_move = move_a;
move_rr_rrr(stacks, final_move, 'e');
}
}
void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range)
@@ -93,16 +75,15 @@ void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range)
i = 0;
while (one_preset->nb_in > 0)
{
if (i > 0)
if (stacks->a)
{
value = get_value_finded(stacks, one_preset, range);
opti_path(stacks, one_preset, range, value);
path_rr_rrr(value, stacks, one_preset, range);
}
normal_move_path(stacks, one_preset, range);
opti2_move_path(stacks, one_preset, range);
sort_little_pile(stacks);
while (stacks->a->value > stacks->a->previous->value)
ra(stacks);
move_ra_rra(stacks->b->value, stacks);
pa(stacks);
put_in_order_ra_rra(stacks);
one_preset->nb_in--;
i++;
if (!check_order(stacks->a))

View File

@@ -1,119 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_utils_tree.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/15 12:20:06 by mteriier #+# #+# */
/* Updated: 2026/01/15 12:20:07 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "medium_headers.h"
#include <stdio.h>
void opti2_move_path(t_stacks *stacks, t_tab *one_preset, int range)
{
int value;
int len;
value = stacks->b->value;
len = stack_a_len(stacks);
if (in_range(value, one_preset->max_range, range) && len > 0)
{
while (value < stacks->a->previous->value && !check_order(stacks->a))
rra(stacks);
}
}
static int get_pre_move_b(t_stacks *stacks, t_tab *one_preset, int range)
{
int i;
t_stack *tmp;
i = 0;
tmp = assign_stack(stacks, 'b');
if (wich_path(stacks, one_preset->max_range, range, 'b'))
{
while (!in_range(tmp->value, one_preset->max_range, range))
{
i++;
tmp = tmp->next;
}
}
else
while (!in_range(tmp->value, one_preset->max_range, range))
{
i++;
tmp = tmp->previous;
}
return (i);
}
static int get_pre_move_a(t_stacks *stacks, int val)
{
int i;
t_stack *tmp;
i = 0;
tmp = assign_stack(stacks, 'a');
if (sort_path(stacks, val))
{
while (val > tmp->value)
{
i++;
tmp = tmp->next;
}
}
else
{
tmp = tmp->previous;
while (val < tmp->value)
{
i++;
tmp = tmp->previous;
}
}
return (i);
}
static int get_min_move(t_stacks *stacks, t_tab *one_preset, int range, int val)
{
int b;
int a;
a = get_pre_move_a(stacks, val);
b = get_pre_move_b(stacks, one_preset, range);
if (a < b)
return (a);
else
return (b);
}
void opti_path(t_stacks *stacks, t_tab *one_preset, int range, int val)
{
int move;
int i;
i = 0;
if (sort_path(stacks, val)
&& wich_path(stacks, one_preset->max_range, range, 'b'))
{
move = get_min_move(stacks, one_preset, range, val);
while (i < move)
{
rr(stacks);
i++;
}
}
else if (!sort_path(stacks, val)
&& !wich_path(stacks, one_preset->max_range, range, 'b'))
{
move = get_min_move(stacks, one_preset, range, val);
while (i < move)
{
rrr(stacks);
i++;
}
}
}

View File

@@ -12,7 +12,7 @@
#include "push_swap.h"
#include "medium_headers.h"
#include <stdio.h>
int get_value_finded(t_stacks *stacks, t_tab *one_preset, int range)
{
int value;
@@ -31,58 +31,6 @@ int get_value_finded(t_stacks *stacks, t_tab *one_preset, int range)
return (value);
}
void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range)
{
if (wich_path(stacks, one_preset->max_range, range, 'b'))
{
while (!in_range(stacks->b->value, one_preset->max_range, range))
rb(stacks);
}
else
{
while (!in_range(stacks->b->value, one_preset->max_range, range))
{
rrb(stacks);
}
}
}
void sort_from_left(t_stacks *stacks)
{
int i;
i = 0;
while (stacks->b->value > stacks->a->value)
{
ra(stacks);
i++;
}
pa(stacks);
while (i > 0)
{
rra(stacks);
i--;
}
}
void sort_from_right(t_stacks *stacks)
{
int i;
i = 0;
while (stacks->b->value < stacks->a->previous->value)
{
rra(stacks);
i++;
}
pa(stacks);
while (i >= 0)
{
ra(stacks);
i--;
}
}
t_stack *assign_stack(t_stacks *stacks, char c)
{
t_stack *tmp;

500
args.txt
View File

@@ -1,500 +0,0 @@
467
484
199
244
257
411
228
384
27
476
402
315
57
353
23
72
35
187
254
103
262
148
239
369
251
334
299
347
290
419
0
223
269
282
150
371
475
130
123
85
311
414
11
256
32
91
47
28
195
265
89
432
452
2
404
366
153
121
160
468
161
126
168
466
86
97
427
326
3
487
87
338
110
227
492
441
417
38
349
261
118
497
443
403
45
117
92
274
410
197
59
145
146
181
433
69
473
119
336
209
454
436
43
132
98
8
293
260
276
264
500
406
51
412
465
439
166
407
382
186
164
182
284
55
36
83
285
106
34
105
243
175
400
494
429
215
397
7
351
280
221
79
26
319
122
499
210
365
66
107
425
234
387
291
364
24
33
287
242
62
178
250
94
288
245
226
211
398
445
191
354
415
305
143
90
281
381
188
238
163
253
458
378
25
189
271
306
61
169
461
17
431
29
70
64
41
490
292
111
177
114
255
184
272
125
225
273
496
252
361
345
206
196
22
304
42
10
379
277
142
453
437
162
60
341
418
266
52
408
219
141
49
450
368
446
258
308
116
128
322
183
423
455
464
198
389
471
459
237
44
489
15
84
179
231
451
50
185
395
317
82
297
493
396
14
263
482
448
312
377
203
320
374
176
216
76
205
140
129
240
332
428
422
330
217
248
200
390
383
156
53
401
135
327
170
40
48
391
328
155
56
74
77
16
30
435
355
301
167
220
235
300
289
172
131
460
246
31
194
247
485
463
359
12
134
95
249
6
307
190
373
409
342
358
88
112
5
426
20
488
120
314
421
9
420
204
78
346
154
13
115
236
63
357
474
37
386
71
495
335
147
101
149
486
385
394
372
65
302
144
449
283
405
214
376
165
356
124
380
1
208
337
102
139
478
229
202
469
375
462
440
333
201
491
457
108
158
316
275
470
350
138
171
137
113
207
267
212
136
295
442
18
151
100
438
157
68
296
241
483
99
96
321
339
39
472
294
329
233
21
259
159
298
133
286
348
230
331
73
109
367
192
416
173
268
213
75
481
58
324
480
370
270
46
278
343
323
363
81
352
325
498
444
180
232
434
392
4
279
218
127
222
174
19
224
424
456
344
54
388
430
362
67
479
360
413
310
80
193
313
477
447
303
309
104
399
318
93
393
340

View File

@@ -13,6 +13,8 @@
#ifndef MEDIUM_HEADERS_H
# define MEDIUM_HEADERS_H
# include "push_swap.h"
typedef struct s_tab
{
int max_range;
@@ -25,13 +27,8 @@ int wich_path(t_stacks *stacks, int max_range, int range, char c);
int stack_len(t_stack *stack);
void bucket_algo(t_stacks *stacks, t_tab *preset, int range);
/* SORT UTILS FILES */
void sort_from_left(t_stacks *stacks);
void sort_from_right(t_stacks *stacks);
void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range);
void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range);
void opti_path(t_stacks *stacks, t_tab *one_preset, int range, int val);
void opti2_move_path(t_stacks *stacks, t_tab *one_preset, int range);
int sort_path(t_stacks *stacks, int value);
t_stack *assign_stack(t_stacks *stacks, char c);
int get_value_finded(t_stacks *stacks, t_tab *one_preset, int range);
/* MEDIUM UTILS FILES */
@@ -43,5 +40,13 @@ int in_range(int value, int max_range, int range);
int get_number_in_range(int max_range, t_stack *a, int range);
/* UTILS STRUCT TAB FILE */
t_tab *get_tabs(t_stack *first, int range);
/* MOVE PATH FILES*/
int get_pre_move_b(t_stacks *stacks, t_tab *one_preset, int range);
int move_left_to_right(int value, t_stacks *stacks, int is_move);
int move_right_to_left(int value, t_stacks *stacks, int is_move);
int wich_path_a(int value, t_stacks *stacks);
void move_ra_rra(int value, t_stacks *stacks);
void path_rr_rrr(int value, t_stacks *stacks, t_tab *one_preset, int range);
void put_in_order_ra_rra(t_stacks *stacks);
#endif