diff --git a/.gitignore b/.gitignore index 505eb4f..3540db9 100644 --- a/.gitignore +++ b/.gitignore @@ -57,7 +57,7 @@ dkms.conf *.swp .vscode *.txt -checker* +checker_Mac push_swap push_swap_debug *.o diff --git a/5_permutations.txt b/5_permutations.txt deleted file mode 100644 index 8add09f..0000000 --- a/5_permutations.txt +++ /dev/null @@ -1,120 +0,0 @@ -1: 0 1 2 3 4 -2: 0 1 2 4 3 -3: 0 1 3 2 4 -4: 0 1 3 4 2 -5: 0 1 4 2 3 -6: 0 1 4 3 2 -7: 0 2 1 3 4 -8: 0 2 1 4 3 -9: 0 2 3 1 4 -10: 0 2 3 4 1 -11: 0 2 4 1 3 -12: 0 2 4 3 1 -13: 0 3 1 2 4 -14: 0 3 1 4 2 -15: 0 3 2 1 4 -16: 0 3 2 4 1 -17: 0 3 4 1 2 -18: 0 3 4 2 1 -19: 0 4 1 2 3 -20: 0 4 1 3 2 -21: 0 4 2 1 3 -22: 0 4 2 3 1 -23: 0 4 3 1 2 -24: 0 4 3 2 1 -25: 1 0 2 3 4 -26: 1 0 2 4 3 -27: 1 0 3 2 4 -28: 1 0 3 4 2 -29: 1 0 4 2 3 -30: 1 0 4 3 2 -31: 1 2 0 3 4 -32: 1 2 0 4 3 -33: 1 2 3 0 4 -34: 1 2 3 4 0 -35: 1 2 4 0 3 -36: 1 2 4 3 0 -37: 1 3 0 2 4 -38: 1 3 0 4 2 -39: 1 3 2 0 4 -40: 1 3 2 4 0 -41: 1 3 4 0 2 -42: 1 3 4 2 0 -43: 1 4 0 2 3 -44: 1 4 0 3 2 -45: 1 4 2 0 3 -46: 1 4 2 3 0 -47: 1 4 3 0 2 -48: 1 4 3 2 0 -49: 2 0 1 3 4 -50: 2 0 1 4 3 -51: 2 0 3 1 4 -52: 2 0 3 4 1 -53: 2 0 4 1 3 -54: 2 0 4 3 1 -55: 2 1 0 3 4 -56: 2 1 0 4 3 -57: 2 1 3 0 4 -58: 2 1 3 4 0 -59: 2 1 4 0 3 -60: 2 1 4 3 0 -61: 2 3 0 1 4 -62: 2 3 0 4 1 -63: 2 3 1 0 4 -64: 2 3 1 4 0 -65: 2 3 4 0 1 -66: 2 3 4 1 0 -67: 2 4 0 1 3 -68: 2 4 0 3 1 -69: 2 4 1 0 3 -70: 2 4 1 3 0 -71: 2 4 3 0 1 -72: 2 4 3 1 0 -73: 3 0 1 2 4 -74: 3 0 1 4 2 -75: 3 0 2 1 4 -76: 3 0 2 4 1 -77: 3 0 4 1 2 -78: 3 0 4 2 1 -79: 3 1 0 2 4 -80: 3 1 0 4 2 -81: 3 1 2 0 4 -82: 3 1 2 4 0 -83: 3 1 4 0 2 -84: 3 1 4 2 0 -85: 3 2 0 1 4 -86: 3 2 0 4 1 -87: 3 2 1 0 4 -88: 3 2 1 4 0 -89: 3 2 4 0 1 -90: 3 2 4 1 0 -91: 3 4 0 1 2 -92: 3 4 0 2 1 -93: 3 4 1 0 2 -94: 3 4 1 2 0 -95: 3 4 2 0 1 -96: 3 4 2 1 0 -97: 4 0 1 2 3 -98: 4 0 1 3 2 -99: 4 0 2 1 3 -100: 4 0 2 3 1 -101: 4 0 3 1 2 -102: 4 0 3 2 1 -103: 4 1 0 2 3 -104: 4 1 0 3 2 -105: 4 1 2 0 3 -106: 4 1 2 3 0 -107: 4 1 3 0 2 -108: 4 1 3 2 0 -109: 4 2 0 1 3 -110: 4 2 0 3 1 -111: 4 2 1 0 3 -112: 4 2 1 3 0 -113: 4 2 3 0 1 -114: 4 2 3 1 0 -115: 4 3 0 1 2 -116: 4 3 0 2 1 -117: 4 3 1 0 2 -118: 4 3 1 2 0 -119: 4 3 2 0 1 -120: 4 3 2 1 0 diff --git a/5_permutations_post_pb.txt b/5_permutations_post_pb.txt deleted file mode 100644 index 4bcd625..0000000 --- a/5_permutations_post_pb.txt +++ /dev/null @@ -1,51 +0,0 @@ -0 1 3 4[2] -0 1 4[3][2] -0 2[1]4[3] -rra-> [3] 0 1 2 4 -pb-> 0 1 2 4 | 3 [SORTED] -rra-> 4 0 1 2 | 3 -pa-> 3 4 0 1 2 -ra x 2-> 0 1 2 3 4 [DONE] -0 2 4[1][3] -0 2 4[3][1] -0 3[2][1]4 -0 4[2]3[1] -0 4[3][2][1] -[1]0 3 4[2] -[1]0 4[3][2] -[1][2]0 4 3 -- will cause problem -- -[1][2]4 0 3 -pb-> 2 4 0 3 | 1 -!in_order->-!try_swap->-pb-> 4 0 3 | 2 1 -in_order-> //insert// -rra; pa-> 2 3 4 0 | 1 -pa-> 1 2 3 4 0 -rra-> [DONE] -[1][3]0 2 4 -[1][3][2]0 4 -1 4[3][2]0 -[2]0 3 4[1] -[2]0 4[3][1] -try_pb->-ra; pb-> 4 3 1 2 | 0 -try_swap->-sa-> 3 4 1 2 | 0 //insert// -rax2; pa-> [DONE] (6 moves) -[2][1]0 4[3] -try_pb-> -[2][1]4 0 3 -[2][3]0 1 4 -[2][3][1]0 4 -2 4[1][3]0 -try_pb->-pb-> 4 1 3 0 | 2 -try_swap->-rra; sa-> 4 0 1 3 | 2 //insert// -rra; pa-> 2 3 4 0 1 -rra x 2-> [DONE] (7 moves) -2 4[3][1]0 -[3]0 2 4[1] -[3]0 4[2][1] -[3][1]0 4[2] -pb-> 1 0 4 2 | 3 -!in_order->-!try_swap->-push_next_b->-rra; pb-> 0 1 4 | 2 3 //insert// -rrr; pa-> 3 4 0 1 | 2 -pa-> 2 3 4 0 1 -rra x2-> [DONE] (8 moves) -[3][1]0 4[2] -try_pb(for each pb, try_swap)->-ra; pb-> 0 4 2 3 | 1 -sa-> 4 0 2 3 | 1 //insert// -ra x2; pa-> 1 2 3 4 0 -rra-> [DONE] (7 moves) -3[1]4 0 2 -[3][2]0 1 4 -[3][2][1]0 4 -3 4[1][2]0 -3 4[2][1]0 -4 0 2 3[1] -4 0 3[2][1] -4[1]0 3[2] -//4[1][3]0 2 -rra x2; pb-> 2 4 1 3 | 0 -rra x 2; pb-> 2 4 3 | 1 0 -sa; ra-> 2 3 4 | 1 0 -pb x 2-> [DONE] (10 moves) -//4[1][3]0 2 -pb-> 1 3 0 2 | 4 -!in_order->-!try_swap->-pb-> 3 0 2 | 1 4 -in_order-> //insert// -rra; pa-> 1 2 3 0 | 4 -ra; pa-> 4 0 1 2 3 -ra-> [DONE] (6 moves) -4[1][3]0 2 -try_pb->-ra; pb-> 3 0 2 4 | 1 -try_swap->-rra; sa-> 3 4 0 2 | 1 //insert// -rra; pa-> 1 2 3 4 0 -rra-> [DONE] (7 moves) -4[2]0 1 3 -try_pb->-pb-> 2 0 1 3 | 4 -try_swap->-rra; sa-> 2 3 0 1 | 4 //insert// -ra x2; pa-> 4 0 1 2 3 -ra-> [DONE] (7 moves) -//4[2][1]0 3 -ra-> [2][1] 0 3 4 -pb-> [1] 0 3 4 | 2 -!in_order->try_swap->-sa-> 0 1 3 4 | 2 //insert// -ra x2-> 3 4 0 1 | 2 -pa-> 2 3 4 0 1 -rra x 2-> [DONE] (8 moves) -//4[2][1]0 3 -pb-> [2][1] 0 3 | 4 -!in_order->!try_swap->--pb-> 1 0 3 | 2 4 -!in_order->sa-> 0 1 3 | 2 4 //insert// -rra; pa -> 2 3 0 1 | 4 --ra x 2; pa-> 4 0 1 2 3 -ra-> [DONE] (9 moves) -//4[2][1]0 3 -ra; pb x 2-> 0 3 4 | 1 2 -in_order-> //insert// -ra; pa-> 1 3 4 0 | 2 -ra pa -> 2 3 4 0 1 -rra x2-> [DONE] (9 moves) -4[2][1]0 3 -pb x 2-> 1 0 3 | 2 4 -!in_order->-sa-> 0 1 3 | 2 4 //insert// -rra; pa-> 2 3 0 1 | 4 -ra x2; pa-> 4 0 1 2 3 -ra->[DONE] (9 moves) -4[3][1][2]0 -//4[3][2][1]0 -pb-> 3 2 1 0 | 4 -!in_order->!try_swap->--pb-> 2 1 0 | 3 4 -!in_order->sa-> 1 2 0 | 3 4 //insert// -ra; pa-> 3 0 1 2 -ra; pa-> 4 0 1 2 3 -ra-> [DONE] (8 moves) -//4[3][2][1]0 -ra; pb-> 2 1 0 4 | 3 -!in_order->!try_swap->--pb-> 1 0 4 | 2 3 -!in_order->-sa-> 0 1 4 | 2 3 //insert// -rra; pa-> 2 4 0 1 | 3 -ra; pa-> 3 4 0 1 2 -rra x2-> [DONE] (10 moves) -//4[3][2][1]0 -!try_pb(for each pb, try_swap)->-pb x2-> 2 1 0 | 3 4 -sa-> 1 2 0 | 3 4 //insert// -ra; pa-> 3 0 1 2 -ra; pa-> 4 0 1 2 3 -ra-> [DONE] (8 moves) -4[3][2][1]0 -pb; sa-> 2 3 1 0 | 4 -ra x2; sa-> 0 1 2 3 -pa-> 4 0 1 2 3 -ra-> [DONE] (7 moves) - -[Legend] -[]: out of place in stack, meaning it is not bigger than the biggest previous one. diff --git a/Makefile b/Makefile index 134b5a1..becf389 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ NAME = push_swap +NAME_BONUS = checker_bonus + # FOR TESTING PURPOSES RUN_ARGS = 37 15 27 4 6 33 12 19 31 3 2 13 49 42 22 16 39 10 24 8 41 5 1 28 7 40 29 47 21 35 50 48 38 45 34 36 44 20 32 17 9 23 25 11 26 14 46 43 30 18 @@ -12,20 +14,25 @@ RED = \033[0;31m ON_RED = \033[41m RESET_COL = \033[0m -CFILES = algorithms.c \ +CFILES = block_utils.c \ calc_insert_cost.c \ - cherrypick.c \ - cherrypick_utils.c \ cleanup.c \ + delta_insert.c \ deques.c \ + deque_modify.c \ deque_operators.c \ + deque_utils.c \ errors.c \ - ignore_LIS.c \ + ignore_lis.c \ + ignore_lis_utils.c \ init.c \ insert.c \ insert_blocks.c \ + insert_blocks_utils.c \ insert_utils.c \ iterators.c \ + longest_increas_subseq.c \ + lis_utils.c \ parse.c \ partition_leaving_vals.c \ partition_stack.c \ @@ -35,14 +42,28 @@ CFILES = algorithms.c \ rrx.c \ rx.c \ sort.c \ - sort_big.c \ sort_utils.c \ + sort_small.c \ split_in_blocks.c \ split_in_two.c \ + split_in_two_utils.c \ sx.c \ - try_x.c \ - undo_moves.c \ - utils.c + undo_moves.c + +CFILES_BONUS = checker_bonus.c \ + checker_utils_bonus.c \ + deques.c \ + deque_modify.c \ + deque_operators.c \ + deque_utils.c \ + errors.c \ + ft_read_line.c \ + px_bonus.c \ + parse_bonus.c \ + rx_bonus.c \ + rrx_bonus.c \ + sx_bonus.c \ + undo_moves.c HFILES = push_swap.h \ deques.h @@ -50,25 +71,16 @@ HFILES = push_swap.h \ SRC_DIR = src SRCS = $(addprefix $(SRC_DIR)/, $(CFILES)) - -OBJ_DIR = obj -OBJS = $(addprefix $(OBJ_DIR)/, $(CFILES:.c=.o)) +OBJ_DIR = obj +OBJS = $(addprefix $(OBJ_DIR)/, $(CFILES:.c=.o)) +OBJS_BONUS = $(addprefix $(OBJ_DIR)/, $(CFILES_BONUS:.c=.o)) INCL = include INCLFLAGS = -I$(INCL) HEADERS = $(addprefix $(INCL)/, $(HFILES)) CC = gcc -CFLAGS = -Wall -Wextra -Werror -g -O3 - -# -# DEBUG build settings -# -DBG_DIR = debug_objs -DBG_EXE = push_swap_try_pb_debug -DBG_OBJS = $(addprefix $(DBG_DIR)/, $(CFILES:.c=.o)) -DBG_CFLAGS = -D DEBUG=1 -g - +CFLAGS = -Wall -Wextra -Werror -O3 LIBFT_DIR = ./libft LIBFT = $(LIBFT_DIR)/libft.a @@ -76,7 +88,7 @@ LIBFT_FLAGS = -lft -Llibft RM_OBJS = rm -rf $(OBJ_DIR) RM_OBJS_OUT = $$($(RM_OBJS) 2>&1 | sed -e 's/error/\\\033[0;31merror\\\033[0m/g' -e 's/warning/\\\033[0;33mwarning\\\033[0m/g') -RM_EXE = rm -f $(NAME) +RM_EXE = rm -f $(NAME) $(NAME_BONUS) RM_EXE_OUT = $$($(RM_EXE) 2>&1 | sed -e 's/error/\\\033[0;31merror\\\033[0m/g' -e 's/warning/\\\033[0;33mwarning\\\033[0m/g') RM_LIBFT = make clean -sC ./libft RM_LIBFT_OUT = $$($(RM_LIBFT) 2>&1 | sed -e 's/error/\\\033[0;31merror\\\033[0m/g' -e 's/warning/\\\033[0;33mwarning\\\033[0m/g') @@ -87,23 +99,15 @@ COMPILE_EXE_OUT = $$($(COMPILE_EXE) 2>&1 | sed -e 's/error/\\\033[0;31merror\\\0 COMPILE_C = $(CC) $(CFLAGS) $(INCLFLAGS) -o $@ -c $< COMPILE_C_OUT = $$($(COMPILE_C) 2>&1 | sed -e 's/error/\\\033[0;31merror\\\033[0m/g' -e 's/warning/\\\033[0;33mwarning\\\033[0m/g') +COMPILE_EXE_BONUS = $(CC) $(CFLAGS) $(LIBFT_FLAGS) $(INCLFLAGS) $(OBJS_BONUS) -o $(NAME_BONUS) +COMPILE_EXE_BONUS_OUT = $$($(COMPILE_EXE_BONUS) 2>&1 | sed -e 's/error/\\\033[0;31merror\\\033[0m/g' -e 's/warning/\\\033[0;33mwarning\\\033[0m/g') + +COMPILE_C_BONUS = $(CC) $(CFLAGS) $(INCLFLAGS) -o $@ -c $< +COMPILE_C_BONUS_OUT = $$($(COMPILE_C_BONUS) 2>&1 | sed -e 's/error/\\\033[0;31merror\\\033[0m/g' -e 's/warning/\\\033[0;33mwarning\\\033[0m/g') + $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c @mkdir -p $(OBJ_DIR) - @printf "$(CYAN)%-30s-->%30s $(RESET_COL)$(COMPILE_C_OUT)\n" $^ $@ - -# -# DEBUG MACROS -# -COMPILE_DBG_EXE = $(CC) $(DBG_CFLAGS) $(LIBFT_FLAGS) $(INCLFLAGS) $(DBG_OBJS) -o $(DBG_EXE) -COMPILE_DBG_EXE_OUT = $$($(COMPILE_DBG_EXE) 2>&1 | sed -e 's/error/\\\033[0;31merror\\\033[0m/g' -e 's/warning/\\\033[0;33mwarning\\\033[0m/g') -COMPILE_DBGC = $(CC) $(DBG_CFLAGS) $(INCLFLAGS) -o $@ -c $< -COMPILE_DBGC_OUT = $$($(COMPILE_DBGC) 2>&1 | sed -e 's/error/\\\033[0;31merror\\\033[0m/g' -e 's/warning/\\\033[0;33mwarning\\\033[0m/g') -RM_DBG_EXE = rm $(DBG_EXE) -RM_DBG_EXE_OUT = $$($(RM_DBG_EXE) 2>&1 | sed -e 's/error/\\\033[0;31merror\\\033[0m/g' -e 's/warning/\\\033[0;33mwarning\\\033[0m/g') - -$(DBG_DIR)/%.o: $(SRC_DIR)/%.c - @mkdir -p $(DBG_DIR) - @echo -e "$(ON_RED)>\t$^\t--> $@ $(RESET_COL)$(COMPILE_DBGC_OUT)" + @printf "$(CYAN)%-32s-->%32s $(RESET_COL)$(COMPILE_C_OUT)\n" $^ $@ all: $(NAME) @if [ -e $(NAME) ]; \ @@ -113,12 +117,23 @@ all: $(NAME) echo -e "$(RED)>>>>>>>> Compilation failed\n>>>>>>>>$(RESET_COL)"; \ fi +bonus: $(NAME_BONUS) + @if [ -e $(NAME_BONUS) ]; \ + then \ + echo -e "$(GREEN)>>>>>>>> Compilation successful\n>>>>>>>>$(RESET_COL)"; \ + else \ + echo -e "$(RED)>>>>>>>> Compilation failed\n>>>>>>>>$(RESET_COL)"; \ + fi + $(NAME): libft pretty_print $(OBJS) @echo -e "\n$(CYAN)>>>>>>>> Compiling $(NAME) ...$(RESET_COL)$(COMPILE_EXE_OUT)" +$(NAME_BONUS): libft pretty_print_bonus $(OBJS_BONUS) + @echo -e "\n$(CYAN)>>>>>>>> Compiling $(NAME_BONUS) ...$(RESET_COL)$(COMPILE_EXE_BONUS_OUT)" + silent_libft: - @echo -e "---------------------- libft.a ----------------------\n" + @echo -e "---------------------- libft ----------------------\n" @echo -e "$(CYAN)>>>>>>>> Archiving libft.a ...$(RESET_COL)" @make -s bonus -C $(LIBFT_DIR) @if [ -e $(LIBFT) ]; \ @@ -133,7 +148,6 @@ libft: silent_libft pretty_print: @echo -e "\n--------------------- $(NAME) ---------------------" - clean: @echo -e "$(RED)>>>>>>>> Deleting obj files$(RESET_COL)$(RM_OBJS_OUT)" @echo -e "$(GREEN)>>>>>>>> obj files deleted\n>>>>>>>>$(RESET_COL)" @@ -142,32 +156,16 @@ clean_libft: @echo -e "$(RED)>>>>>>>> make fclean -sC libft $(RESET_COL)$(RM_LIBFT_OUT)" @echo -e "$(GREEN)>>>>>>>> libft cleaned\n>>>>>>>>$(RESET_COL)" -clean_debug: clean - @echo -e "$(RED)>>>>>>>> Deleting debug obj files$(RESET_COL)$(RM_DBG_EXE_OUT)" - @echo -e "$(GREEN)>>>>>>>> obj files deleted\n>>>>>>>>$(RESET_COL)" - -fclean: clean clean_libft clean_debug +fclean: clean clean_libft @echo -e "$(RED)>>>>>>>> Deleting $(NAME)$(RESET_COL)$(RM_EXE_OUT)" @echo -e "$(GREEN)>>>>>>>> ./$(NAME) deleted\n>>>>>>>>$(RESET_COL)" re: fclean all -test5: all - gcc -g push_swap_tester.c libft/libft.a && ./a.out 5; - -bonus: all +pretty_print_bonus: + @echo -e "$(GREEN)\n------------------- $(NAME_BONUS) -------------------$(RESET_COL)\n" run: all ./$(NAME) $(RUN_ARGS) -# -# Debug rules -# -pretty_print_debug: - @echo -e "$(RED)\n------------------- $(DBG_EXE) -------------------$(RESET_COL)\n" - -debug: all pretty_print_debug $(DBG_OBJS) - @echo -e "\n$(ON_RED)>>>>>>>> Compiling $(DBG_EXE) ...$(RESET_COL)$(COMPILE_DBG_EXE_OUT)" - ./$(DBG_EXE) $(RUN_ARGS) - -.PHONY: all clean clean_libft fclean re bonus libft silent_libft pretty_print pretty_print_debug run debug test5 +.PHONY: all clean clean_libft fclean re bonus libft silent_libft pretty_print pretty_print_bonus run diff --git a/REMINDER b/REMINDER deleted file mode 100644 index 0287710..0000000 --- a/REMINDER +++ /dev/null @@ -1,44 +0,0 @@ - ___ | - / _ \ | -| (_) | | - \___/ | - _ __ | __ _ -| '_ \ | / _` | -| | | | | | (_| | -|_| |_| | \__,_| - ___ | _ - / _ \ | | | -| __/ | | |_ - \___| | | __| - | | |_ - _ | \__| - | | | - | |_ | - | __| | __ _ - | |_ | / _` | - \__| | | (_| | - __ _ | \__,_| - / _` | | -| (_| | | - \__,_| | _ - ___ | | | - / __| | | |_ - \__ \ | | __| - |___/ | | |_ - _ | \__| -| | | _ -| | __ | (_) -| |/ / | _ -| < | | | -|_|\_\ | | | - | |_| - | - | _ __ ___ - | | '_ ` _ \ - | | | | | | | - | |_| |_| |_| - | - | ___ - | / _ \ - | | __/ - | \___| diff --git a/backup/diff_algo.log b/backup/diff_algo.log deleted file mode 100644 index e681f4d..0000000 --- a/backup/diff_algo.log +++ /dev/null @@ -1,80 +0,0 @@ -12c12 -< 8 ---- -> 6 -34c34 -< 7 ---- -> 4 -78c78 -< 7 ---- -> 8 -86c86 -< 6 ---- -> 5 -96c96 -< 7 ---- -> 9 -98c98 -< 5 ---- -> 6 -108c108 -< 6 ---- -> 10 -144c144 -< 6 ---- -> 9 -156c156 -< 5 ---- -> 9 -160c160 -< 7 ---- -> 10 -192c192 -< 9 ---- -> 8 -204c204 -< 9 ---- -> 7 -208c208 -< 6 ---- -> 8 -222c222 -< 8 ---- -> 9 -226c226 -< 7 ---- -> 5 -242d241 -< 0 1 4 3 2 --> 8 -244a244,245 -> 1 3 2 0 4 --> 8 -> 1 4 3 2 0 --> 9 -245a247 -> 2 0 4 3 1 --> 10 -250a253 -> 2 4 3 1 0 --> 9 -252a256,257 -> 3 0 4 2 1 --> 9 -> 3 1 0 4 2 --> 10 -256,258c261,263 -< 3 4 2 1 0 --> 9 -< 4 0 3 2 1 --> 9 -< 4 2 1 0 3 --> 8 ---- -> 3 4 2 1 0 --> 8 -> 4 1 0 3 2 --> 8 -> 4 2 1 0 3 --> 9 diff --git a/backup/make_moves.c b/backup/make_moves.c deleted file mode 100644 index a6b26bb..0000000 --- a/backup/make_moves.c +++ /dev/null @@ -1,25 +0,0 @@ -#include "push_swap.h" - -void make_rab(int **stack, int size, int move) -{ - rotate(stack, size); - print_move(move); -} - -void make_rrab(int **stack, int size, int move) -{ - rev_rotate(stack, size); - print_move(move); -} - -void make_sab(int *stack, int size, int move) -{ - swap(stack, size); - print_move(move); -} - -void make_push(t_main_cont *cont, int move) -{ - push(cont, move); - print_move(move); -} diff --git a/backup/moves.c b/backup/moves.c deleted file mode 100644 index e83d84b..0000000 --- a/backup/moves.c +++ /dev/null @@ -1,47 +0,0 @@ -#include "push_swap.h" - -void rotate(int **stack, int size) -{ - *(*stack + size) = *stack[0]; - *stack[0] = 0; - *stack += 1; - return ; -} - -void rev_rotate(int **stack, int size) -{ - *(*stack - 1) = *(*stack + size - 1); - *(*stack + size) = 0; - *stack -= 1; - return ; -} - -void swap(int *stack, int size) -{ - int temp; - - if (size <= 1) - return ; - temp = stack[0]; - stack[0] = stack[1]; - stack[1] = temp; -} - -static void private_push(int **stack_src, int **stack_dest, \ - int *size_src, int *size_dest) -{ - *(*stack_dest - 1) = *stack_src[0]; - *stack_src[0] = 0; - *size_dest += 1; - *size_src -= 1; - *stack_dest -= 1; - *stack_src += 1; -} - -void push(t_main_cont *cont, int move) -{ - if (move == PA) - private_push(&cont->B, &cont->A, &cont->sizeB, &cont->sizeA); - else if (move == PB) - private_push(&cont->A, &cont->B, &cont->sizeA, &cont->sizeB); -} diff --git a/backup/no_try_pb.log b/backup/no_try_pb.log deleted file mode 100644 index 6456109..0000000 --- a/backup/no_try_pb.log +++ /dev/null @@ -1,249 +0,0 @@ -0 1 2 3 4: OK - 0 -0 1 2 4 3: OK - 5 -0 1 3 2 4: OK - 5 -0 1 3 4 2: OK - 4 -0 1 4 2 3: OK - 4 -0 1 4 3 2: OK - 6 -0 2 1 3 4: OK - 3 -0 2 1 4 3: OK - 7 -0 2 3 1 4: OK - 6 -0 2 3 4 1: OK - 2 -0 2 4 1 3: OK - 5 -0 2 4 3 1: OK - 6 -0 3 1 2 4: OK - 6 -0 3 1 4 2: OK - 5 -0 3 2 1 4: OK - 8 -0 3 2 4 1: OK - 4 -0 3 4 1 2: OK - 4 -0 3 4 2 1: OK - 5 -0 4 1 2 3: OK - 2 -0 4 1 3 2: OK - 6 -0 4 2 1 3: OK - 4 -0 4 2 3 1: OK - 5 -0 4 3 1 2: OK - 5 -0 4 3 2 1: OK - 7 -1 0 2 3 4: OK - 1 -1 0 2 4 3: OK - 7 -1 0 3 2 4: OK - 5 -1 0 3 4 2: OK - 6 -1 0 4 2 3: OK - 6 -1 0 4 3 2: OK - 8 -1 2 0 3 4: OK - 5 -1 2 0 4 3: OK - 7 -1 2 3 0 4: OK - 4 -1 2 3 4 0: OK - 1 -1 2 4 0 3: OK - 5 -1 2 4 3 0: OK - 5 -1 3 0 2 4: OK - 6 -1 3 0 4 2: OK - 7 -1 3 2 0 4: OK - 8 -1 3 2 4 0: OK - 4 -1 3 4 0 2: OK - 3 -1 3 4 2 0: OK - 7 -1 4 0 2 3: OK - 5 -1 4 0 3 2: OK - 6 -1 4 2 0 3: OK - 6 -1 4 2 3 0: OK - 7 -1 4 3 0 2: OK - 5 -1 4 3 2 0: OK - 9 -2 0 1 3 4: OK - 6 -2 0 1 4 3: OK - 7 -2 0 3 1 4: OK - 7 -2 0 3 4 1: OK - 8 -2 0 4 1 3: OK - 6 -2 0 4 3 1: OK - 10 -2 1 0 3 4: OK - 7 -2 1 0 4 3: OK - 9 -2 1 3 0 4: OK - 8 -2 1 3 4 0: OK - 2 -2 1 4 0 3: OK - 7 -2 1 4 3 0: OK - 6 -2 3 0 1 4: OK - 6 -2 3 0 4 1: OK - 4 -2 3 1 0 4: OK - 8 -2 3 1 4 0: OK - 6 -2 3 4 0 1: OK - 2 -2 3 4 1 0: OK - 3 -2 4 0 1 3: OK - 4 -2 4 0 3 1: OK - 8 -2 4 1 0 3: OK - 8 -2 4 1 3 0: OK - 7 -2 4 3 0 1: OK - 4 -2 4 3 1 0: OK - 9 -3 0 1 2 4: OK - 4 -3 0 1 4 2: OK - 8 -3 0 2 1 4: OK - 8 -3 0 2 4 1: OK - 7 -3 0 4 1 2: OK - 3 -3 0 4 2 1: OK - 9 -3 1 0 2 4: OK - 6 -3 1 0 4 2: OK - 10 -3 1 2 0 4: OK - 7 -3 1 2 4 0: OK - 6 -3 1 4 0 2: OK - 8 -3 1 4 2 0: OK - 7 -3 2 0 1 4: OK - 7 -3 2 0 4 1: OK - 6 -3 2 1 0 4: OK - 9 -3 2 1 4 0: OK - 7 -3 2 4 0 1: OK - 3 -3 2 4 1 0: OK - 8 -3 4 0 1 2: OK - 2 -3 4 0 2 1: OK - 4 -3 4 1 0 2: OK - 3 -3 4 1 2 0: OK - 6 -3 4 2 0 1: OK - 6 -3 4 2 1 0: OK - 8 -4 0 1 2 3: OK - 1 -4 0 1 3 2: OK - 5 -4 0 2 1 3: OK - 4 -4 0 2 3 1: OK - 5 -4 0 3 1 2: OK - 5 -4 0 3 2 1: OK - 7 -4 1 0 2 3: OK - 2 -4 1 0 3 2: OK - 8 -4 1 2 0 3: OK - 7 -4 1 2 3 0: OK - 3 -4 1 3 0 2: OK - 6 -4 1 3 2 0: OK - 7 -4 2 0 1 3: OK - 7 -4 2 0 3 1: OK - 6 -4 2 1 0 3: OK - 9 -4 2 1 3 0: OK - 5 -4 2 3 0 1: OK - 5 -4 2 3 1 0: OK - 6 -4 3 0 1 2: OK - 3 -4 3 0 2 1: OK - 7 -4 3 1 0 2: OK - 5 -4 3 1 2 0: OK - 6 -4 3 2 0 1: OK - 6 -4 3 2 1 0: OK - 8 -** stacks resulting in nb_moves > 8 : ** - 1 4 3 2 0 --> 9 - 2 0 4 3 1 --> 10 - 2 1 0 4 3 --> 9 - 2 4 3 1 0 --> 9 - 3 0 4 2 1 --> 9 - 3 1 0 4 2 --> 10 - 3 2 1 0 4 --> 9 - 4 2 1 0 3 --> 9 diff --git a/backup/normalize.c b/backup/normalize.c deleted file mode 100644 index e26b795..0000000 --- a/backup/normalize.c +++ /dev/null @@ -1,30 +0,0 @@ -#include "push_swap.h" - -int *normalize_stack_values(int *src_stack, int size) -{ - int i; - int stack_pos; - int nb_smaller_elements; - int *normalized_stack; - - normalized_stack = malloc(size * sizeof(int)); - if (!normalized_stack) - exit_on_err("Malloc error\n"); - stack_pos = 0; - while (stack_pos < size) - { - i = 0; - nb_smaller_elements = 0; - while (i < size) - { - if (src_stack[i] < src_stack[stack_pos]) - nb_smaller_elements++; - i++; - } - normalized_stack[stack_pos] = nb_smaller_elements; - stack_pos++; - } - src_stack = ft_memcpy(src_stack, normalized_stack, size * sizeof(int)); - free(normalized_stack); - return (src_stack); -} diff --git a/backup/print_utils.c b/backup/print_utils.c deleted file mode 100644 index ed880a5..0000000 --- a/backup/print_utils.c +++ /dev/null @@ -1,69 +0,0 @@ -#include "push_swap.h" - -void print_single_stack(int *stack, int size) -{ - int i; - - printf(" |\033[38;5;5m [A] \033[0m|\n"); - printf("+---+-----+\n"); - i = 0; - while (i < size) - { - printf("|%-3d|", i); - if (i < size) - printf("\033[38;5;5m %-3d \033[0;0;0m|\n", stack[i]); - else - printf("\t\033[0m|\n"); - i++; - } - printf("+---+-----+\n"); -} - -void print_stacks(t_main_cont *cont) -{ - int i; - - printf(" |\033[38;5;5m [A] \033[0m|\033[38;5;6m [B] \033[0;0;0m|\n"); - printf("+---+-----+-----+\n"); - i = 0; - while (i < cont->sizeA || i < cont->sizeB) - { - printf("|%-3d|", i); - if (i < cont->sizeA) - printf("\033[38;5;5m %-3d \033[0;0;0m", cont->A[i]); - else - printf("\t\033[0m|"); - if (i < cont->sizeB) - printf("|\033[38;5;6m %-3d \033[0m|\n", cont->B[i]); - else - printf("| |\n\033[0m"); - i++; - } - printf("+---+-----+-----+\n"); -} - -void print_move(int move) -{ - if (move == RA) - write(1, "ra\n", 3); - else if (move == RB) - write(1, "rb\n", 3); - else if (move == RR) - write(1, "rr\n", 3); - else if (move == RRA) - write(1, "rra\n", 4); - else if (move == RRB) - write(1, "rrb\n", 4); - else if (move == RRR) - write(1, "rrr\n", 4); - else if (move == SA) - write(1, "sa\n", 3); - else if (move == SB) - write(1, "sb\n", 3); - else if (move == SS) - write(1, "ss\n", 3); - else if (move == PA) - write(1, "pa\n", 3); - else if (move == PB) - write(1, "pb\n", 3); -} diff --git a/backup/push_swap_no_try_pb b/backup/push_swap_no_try_pb deleted file mode 100755 index 0bf048d..0000000 Binary files a/backup/push_swap_no_try_pb and /dev/null differ diff --git a/backup/push_swap_no_try_pb_debug b/backup/push_swap_no_try_pb_debug deleted file mode 100755 index ff8ce85..0000000 Binary files a/backup/push_swap_no_try_pb_debug and /dev/null differ diff --git a/backup/push_swap_try_pb b/backup/push_swap_try_pb deleted file mode 100755 index fc42e84..0000000 Binary files a/backup/push_swap_try_pb and /dev/null differ diff --git a/backup/push_swap_try_pb_debug b/backup/push_swap_try_pb_debug deleted file mode 100755 index e11ba8f..0000000 Binary files a/backup/push_swap_try_pb_debug and /dev/null differ diff --git a/backup/results_5.log b/backup/results_5.log deleted file mode 100644 index 66494e4..0000000 --- a/backup/results_5.log +++ /dev/null @@ -1,964 +0,0 @@ -0 1 2 3 4: OK - 0 -0 1 2 4 3: OK - 5 -0 1 3 2 4: OK - 5 -0 1 3 4 2: KO - 0 -0 1 4 2 3: KO - 0 -0 1 4 3 2: KO - 0 -0 2 1 3 4: OK - 3 -0 2 1 4 3: KO - 0 -0 2 3 1 4: KO - 0 -0 2 3 4 1: OK - 2 -0 2 4 1 3: KO - 0 -0 2 4 3 1: KO - 0 -0 3 1 2 4: KO - 0 -0 3 1 4 2: KO - 0 -0 3 2 1 4: KO - 0 -0 3 2 4 1: KO - 0 -0 3 4 1 2: KO - 0 -0 3 4 2 1: KO - 0 -0 4 1 2 3: OK - 2 -0 4 1 3 2: KO - 0 -0 4 2 1 3: KO - 0 -0 4 2 3 1: KO - 0 -0 4 3 1 2: KO - 0 -0 4 3 2 1: KO - 0 -1 0 2 3 4: OK - 1 -1 0 2 4 3: KO - 0 -1 0 3 2 4: KO - 0 -1 0 3 4 2: KO - 0 -1 0 4 2 3: KO - 0 -1 0 4 3 2: KO - 0 -1 2 0 3 4: KO - 0 -1 2 0 4 3: KO - 0 -1 2 3 0 4: OK - 4 -1 2 3 4 0: OK - 1 -1 2 4 0 3: KO - 0 -1 2 4 3 0: OK - 5 -1 3 0 2 4: KO - 0 -1 3 0 4 2: KO - 0 -1 3 2 0 4: KO - 0 -1 3 2 4 0: OK - 4 -1 3 4 0 2: OK - 3 -1 3 4 2 0: KO - 0 -1 4 0 2 3: KO - 0 -1 4 0 3 2: KO - 0 -1 4 2 0 3: KO - 0 -1 4 2 3 0: KO - 0 -1 4 3 0 2: KO - 0 -1 4 3 2 0: KO - 0 -2 0 1 3 4: KO - 0 -2 0 1 4 3: KO - 0 -2 0 3 1 4: KO - 0 -2 0 3 4 1: KO - 0 -2 0 4 1 3: KO - 0 -2 0 4 3 1: KO - 0 -2 1 0 3 4: KO - 0 -2 1 0 4 3: KO - 0 -2 1 3 0 4: KO - 0 -2 1 3 4 0: OK - 2 -2 1 4 0 3: KO - 0 -2 1 4 3 0: KO - 0 -2 3 0 1 4: KO - 0 -2 3 0 4 1: OK - 4 -2 3 1 0 4: KO - 0 -2 3 1 4 0: KO - 0 -2 3 4 0 1: OK - 2 -2 3 4 1 0: OK - 3 -2 4 0 1 3: OK - 4 -2 4 0 3 1: KO - 0 -2 4 1 0 3: KO - 0 -2 4 1 3 0: KO - 0 -2 4 3 0 1: OK - 4 -2 4 3 1 0: KO - 0 -3 0 1 2 4: OK - 4 -3 0 1 4 2: KO - 0 -3 0 2 1 4: KO - 0 -3 0 2 4 1: KO - 0 -3 0 4 1 2: OK - 3 -3 0 4 2 1: KO - 0 -3 1 0 2 4: KO - 0 -3 1 0 4 2: KO - 0 -3 1 2 0 4: KO - 0 -3 1 2 4 0: KO - 0 -3 1 4 0 2: KO - 0 -3 1 4 2 0: KO - 0 -3 2 0 1 4: KO - 0 -3 2 0 4 1: KO - 0 -3 2 1 0 4: KO - 0 -3 2 1 4 0: KO - 0 -3 2 4 0 1: OK - 3 -3 2 4 1 0: KO - 0 -3 4 0 1 2: OK - 2 -3 4 0 2 1: OK - 4 -3 4 1 0 2: OK - 3 -3 4 1 2 0: KO - 0 -3 4 2 0 1: KO - 0 -3 4 2 1 0: KO - 0 -4 0 1 2 3: OK - 1 -4 0 1 3 2: OK - 5 -4 0 2 1 3: OK - 4 -4 0 2 3 1: KO - 0 -4 0 3 1 2: KO - 0 -4 0 3 2 1: KO - 0 -4 1 0 2 3: OK - 2 -4 1 0 3 2: KO - 0 -4 1 2 0 3: KO - 0 -4 1 2 3 0: OK - 3 -4 1 3 0 2: KO - 0 -4 1 3 2 0: KO - 0 -4 2 0 1 3: KO - 0 -4 2 0 3 1: KO - 0 -4 2 1 0 3: KO - 0 -4 2 1 3 0: KO - 0 -4 2 3 0 1: KO - 0 -4 2 3 1 0: KO - 0 -4 3 0 1 2: OK - 3 -4 3 0 2 1: KO - 0 -4 3 1 0 2: KO - 0 -4 3 1 2 0: KO - 0 -4 3 2 0 1: KO - 0 -4 3 2 1 0: KO - 0 -** stacks resulting in nb_moves > 7 : ** -0 1 2 3 4: OK - 0 -0 1 2 4 3: OK - 5 -0 1 3 2 4: OK - 5 -0 1 3 4 2: KO - 0 -0 1 4 2 3: KO - 0 -0 1 4 3 2: KO - 0 -0 2 1 3 4: OK - 3 -0 2 1 4 3: KO - 0 -0 2 3 1 4: KO - 0 -0 2 3 4 1: OK - 2 -0 2 4 1 3: KO - 0 -0 2 4 3 1: KO - 0 -0 3 1 2 4: KO - 0 -0 3 1 4 2: KO - 0 -0 3 2 1 4: KO - 0 -0 3 2 4 1: KO - 0 -0 3 4 1 2: KO - 0 -0 3 4 2 1: KO - 0 -0 4 1 2 3: OK - 2 -0 4 1 3 2: KO - 0 -0 4 2 1 3: KO - 0 -0 4 2 3 1: KO - 0 -0 4 3 1 2: KO - 0 -0 4 3 2 1: KO - 0 -1 0 2 3 4: OK - 1 -1 0 2 4 3: KO - 0 -1 0 3 2 4: KO - 0 -1 0 3 4 2: KO - 0 -1 0 4 2 3: KO - 0 -1 0 4 3 2: KO - 0 -1 2 0 3 4: KO - 0 -1 2 0 4 3: KO - 0 -1 2 3 0 4: OK - 4 -1 2 3 4 0: OK - 1 -1 2 4 0 3: KO - 0 -1 2 4 3 0: OK - 5 -1 3 0 2 4: KO - 0 -1 3 0 4 2: KO - 0 -1 3 2 0 4: KO - 0 -1 3 2 4 0: OK - 4 -1 3 4 0 2: OK - 3 -1 3 4 2 0: KO - 0 -1 4 0 2 3: KO - 0 -1 4 0 3 2: KO - 0 -1 4 2 0 3: KO - 0 -1 4 2 3 0: KO - 0 -1 4 3 0 2: KO - 0 -1 4 3 2 0: KO - 0 -2 0 1 3 4: KO - 0 -2 0 1 4 3: KO - 0 -2 0 3 1 4: KO - 0 -2 0 3 4 1: KO - 0 -2 0 4 1 3: KO - 0 -2 0 4 3 1: KO - 0 -2 1 0 3 4: KO - 0 -2 1 0 4 3: KO - 0 -2 1 3 0 4: KO - 0 -2 1 3 4 0: OK - 2 -2 1 4 0 3: KO - 0 -2 1 4 3 0: KO - 0 -2 3 0 1 4: KO - 0 -2 3 0 4 1: OK - 4 -2 3 1 0 4: KO - 0 -2 3 1 4 0: KO - 0 -2 3 4 0 1: OK - 2 -2 3 4 1 0: OK - 3 -2 4 0 1 3: OK - 4 -2 4 0 3 1: KO - 0 -2 4 1 0 3: KO - 0 -2 4 1 3 0: KO - 0 -2 4 3 0 1: OK - 4 -2 4 3 1 0: KO - 0 -3 0 1 2 4: OK - 4 -3 0 1 4 2: KO - 0 -3 0 2 1 4: KO - 0 -3 0 2 4 1: KO - 0 -3 0 4 1 2: OK - 3 -3 0 4 2 1: KO - 0 -3 1 0 2 4: KO - 0 -3 1 0 4 2: KO - 0 -3 1 2 0 4: KO - 0 -3 1 2 4 0: KO - 0 -3 1 4 0 2: KO - 0 -3 1 4 2 0: KO - 0 -3 2 0 1 4: KO - 0 -3 2 0 4 1: KO - 0 -3 2 1 0 4: KO - 0 -3 2 1 4 0: KO - 0 -3 2 4 0 1: OK - 3 -3 2 4 1 0: KO - 0 -3 4 0 1 2: OK - 2 -3 4 0 2 1: OK - 4 -3 4 1 0 2: OK - 3 -3 4 1 2 0: KO - 0 -3 4 2 0 1: KO - 0 -3 4 2 1 0: KO - 0 -4 0 1 2 3: OK - 1 -4 0 1 3 2: OK - 5 -4 0 2 1 3: OK - 4 -4 0 2 3 1: KO - 0 -4 0 3 1 2: KO - 0 -4 0 3 2 1: KO - 0 -4 1 0 2 3: OK - 2 -4 1 0 3 2: KO - 0 -4 1 2 0 3: KO - 0 -4 1 2 3 0: OK - 3 -4 1 3 0 2: KO - 0 -4 1 3 2 0: KO - 0 -4 2 0 1 3: KO - 0 -4 2 0 3 1: KO - 0 -4 2 1 0 3: KO - 0 -4 2 1 3 0: KO - 0 -4 2 3 0 1: KO - 0 -4 2 3 1 0: KO - 0 -4 3 0 1 2: OK - 3 -4 3 0 2 1: KO - 0 -4 3 1 0 2: KO - 0 -4 3 1 2 0: KO - 0 -4 3 2 0 1: KO - 0 -4 3 2 1 0: KO - 0 -** stacks resulting in nb_moves > 7 : ** -0 1 2 3 4: OK - 0 -0 1 2 4 3: OK - 5 -0 1 3 2 4: OK - 5 -0 1 3 4 2: KO - 0 -0 1 4 2 3: KO - 0 -0 1 4 3 2: KO - 0 -0 2 1 3 4: OK - 3 -0 2 1 4 3: KO - 0 -0 2 3 1 4: KO - 0 -0 2 3 4 1: OK - 2 -0 2 4 1 3: KO - 0 -0 2 4 3 1: KO - 0 -0 3 1 2 4: KO - 0 -0 3 1 4 2: KO - 0 -0 3 2 1 4: KO - 0 -0 3 2 4 1: KO - 0 -0 3 4 1 2: KO - 0 -0 3 4 2 1: KO - 0 -0 4 1 2 3: OK - 2 -0 4 1 3 2: KO - 0 -0 4 2 1 3: KO - 0 -0 4 2 3 1: KO - 0 -0 4 3 1 2: KO - 0 -0 4 3 2 1: KO - 0 -1 0 2 3 4: OK - 1 -1 0 2 4 3: KO - 0 -1 0 3 2 4: KO - 0 -1 0 3 4 2: KO - 0 -1 0 4 2 3: KO - 0 -1 0 4 3 2: KO - 0 -1 2 0 3 4: KO - 0 -1 2 0 4 3: KO - 0 -1 2 3 0 4: OK - 4 -1 2 3 4 0: OK - 1 -1 2 4 0 3: KO - 0 -1 2 4 3 0: OK - 5 -1 3 0 2 4: KO - 0 -1 3 0 4 2: KO - 0 -1 3 2 0 4: KO - 0 -1 3 2 4 0: OK - 4 -1 3 4 0 2: OK - 3 -1 3 4 2 0: KO - 0 -1 4 0 2 3: KO - 0 -1 4 0 3 2: KO - 0 -1 4 2 0 3: KO - 0 -1 4 2 3 0: KO - 0 -1 4 3 0 2: KO - 0 -1 4 3 2 0: KO - 0 -2 0 1 3 4: KO - 0 -2 0 1 4 3: KO - 0 -2 0 3 1 4: KO - 0 -2 0 3 4 1: KO - 0 -2 0 4 1 3: KO - 0 -2 0 4 3 1: KO - 0 -2 1 0 3 4: KO - 0 -2 1 0 4 3: KO - 0 -2 1 3 0 4: KO - 0 -2 1 3 4 0: OK - 2 -2 1 4 0 3: KO - 0 -2 1 4 3 0: KO - 0 -2 3 0 1 4: KO - 0 -2 3 0 4 1: OK - 4 -2 3 1 0 4: KO - 0 -2 3 1 4 0: KO - 0 -2 3 4 0 1: OK - 2 -2 3 4 1 0: OK - 3 -2 4 0 1 3: OK - 4 -2 4 0 3 1: KO - 0 -2 4 1 0 3: KO - 0 -2 4 1 3 0: KO - 0 -2 4 3 0 1: OK - 4 -2 4 3 1 0: KO - 0 -3 0 1 2 4: OK - 4 -3 0 1 4 2: KO - 0 -3 0 2 1 4: KO - 0 -3 0 2 4 1: KO - 0 -3 0 4 1 2: OK - 3 -3 0 4 2 1: KO - 0 -3 1 0 2 4: KO - 0 -3 1 0 4 2: KO - 0 -3 1 2 0 4: KO - 0 -3 1 2 4 0: KO - 0 -3 1 4 0 2: KO - 0 -3 1 4 2 0: KO - 0 -3 2 0 1 4: KO - 0 -3 2 0 4 1: KO - 0 -3 2 1 0 4: KO - 0 -3 2 1 4 0: KO - 0 -3 2 4 0 1: OK - 3 -3 2 4 1 0: KO - 0 -3 4 0 1 2: OK - 2 -3 4 0 2 1: OK - 4 -3 4 1 0 2: OK - 3 -3 4 1 2 0: KO - 0 -3 4 2 0 1: KO - 0 -3 4 2 1 0: KO - 0 -4 0 1 2 3: OK - 1 -4 0 1 3 2: OK - 5 -4 0 2 1 3: OK - 4 -4 0 2 3 1: KO - 0 -4 0 3 1 2: KO - 0 -4 0 3 2 1: KO - 0 -4 1 0 2 3: OK - 2 -4 1 0 3 2: KO - 0 -4 1 2 0 3: KO - 0 -4 1 2 3 0: OK - 3 -4 1 3 0 2: KO - 0 -4 1 3 2 0: KO - 0 -4 2 0 1 3: KO - 0 -4 2 0 3 1: KO - 0 -4 2 1 0 3: KO - 0 -4 2 1 3 0: KO - 0 -4 2 3 0 1: KO - 0 -4 2 3 1 0: KO - 0 -4 3 0 1 2: OK - 3 -4 3 0 2 1: KO - 0 -4 3 1 0 2: KO - 0 -4 3 1 2 0: KO - 0 -4 3 2 0 1: KO - 0 -4 3 2 1 0: KO - 0 -** stacks resulting in nb_moves > 7 : ** -0 1 2 3 4: OK - 0 -0 1 2 4 3: OK - 5 -0 1 3 2 4: OK - 5 -0 1 3 4 2: KO - 0 -0 1 4 2 3: KO - 0 -0 1 4 3 2: KO - 0 -0 2 1 3 4: OK - 3 -0 2 1 4 3: KO - 0 -0 2 3 1 4: KO - 0 -0 2 3 4 1: OK - 2 -0 2 4 1 3: KO - 0 -0 2 4 3 1: KO - 0 -0 3 1 2 4: KO - 0 -0 3 1 4 2: KO - 0 -0 3 2 1 4: KO - 0 -0 3 2 4 1: KO - 0 -0 3 4 1 2: KO - 0 -0 3 4 2 1: KO - 0 -0 4 1 2 3: OK - 2 -0 4 1 3 2: KO - 0 -0 4 2 1 3: KO - 0 -0 4 2 3 1: KO - 0 -0 4 3 1 2: KO - 0 -0 4 3 2 1: KO - 0 -1 0 2 3 4: OK - 1 -1 0 2 4 3: KO - 0 -1 0 3 2 4: KO - 0 -1 0 3 4 2: KO - 0 -1 0 4 2 3: KO - 0 -1 0 4 3 2: KO - 0 -1 2 0 3 4: KO - 0 -1 2 0 4 3: KO - 0 -1 2 3 0 4: OK - 4 -1 2 3 4 0: OK - 1 -1 2 4 0 3: KO - 0 -1 2 4 3 0: OK - 5 -1 3 0 2 4: KO - 0 -1 3 0 4 2: KO - 0 -1 3 2 0 4: KO - 0 -1 3 2 4 0: OK - 4 -1 3 4 0 2: OK - 3 -1 3 4 2 0: KO - 0 -1 4 0 2 3: KO - 0 -1 4 0 3 2: KO - 0 -1 4 2 0 3: KO - 0 -1 4 2 3 0: KO - 0 -1 4 3 0 2: KO - 0 -1 4 3 2 0: KO - 0 -2 0 1 3 4: KO - 0 -2 0 1 4 3: KO - 0 -2 0 3 1 4: KO - 0 -2 0 3 4 1: KO - 0 -2 0 4 1 3: KO - 0 -2 0 4 3 1: KO - 0 -2 1 0 3 4: KO - 0 -2 1 0 4 3: KO - 0 -2 1 3 0 4: KO - 0 -2 1 3 4 0: OK - 2 -2 1 4 0 3: KO - 0 -2 1 4 3 0: KO - 0 -2 3 0 1 4: KO - 0 -2 3 0 4 1: OK - 4 -2 3 1 0 4: KO - 0 -2 3 1 4 0: KO - 0 -2 3 4 0 1: OK - 2 -2 3 4 1 0: OK - 3 -2 4 0 1 3: OK - 4 -2 4 0 3 1: KO - 0 -2 4 1 0 3: KO - 0 -2 4 1 3 0: KO - 0 -2 4 3 0 1: OK - 4 -2 4 3 1 0: KO - 0 -3 0 1 2 4: OK - 4 -3 0 1 4 2: KO - 0 -3 0 2 1 4: KO - 0 -3 0 2 4 1: KO - 0 -3 0 4 1 2: OK - 3 -3 0 4 2 1: KO - 0 -3 1 0 2 4: KO - 0 -3 1 0 4 2: KO - 0 -3 1 2 0 4: KO - 0 -3 1 2 4 0: KO - 0 -3 1 4 0 2: KO - 0 -3 1 4 2 0: KO - 0 -3 2 0 1 4: KO - 0 -3 2 0 4 1: KO - 0 -3 2 1 0 4: KO - 0 -3 2 1 4 0: KO - 0 -3 2 4 0 1: OK - 3 -3 2 4 1 0: KO - 0 -3 4 0 1 2: OK - 2 -3 4 0 2 1: OK - 4 -3 4 1 0 2: OK - 3 -3 4 1 2 0: KO - 0 -3 4 2 0 1: KO - 0 -3 4 2 1 0: KO - 0 -4 0 1 2 3: OK - 1 -4 0 1 3 2: OK - 5 -4 0 2 1 3: OK - 4 -4 0 2 3 1: KO - 0 -4 0 3 1 2: KO - 0 -4 0 3 2 1: KO - 0 -4 1 0 2 3: OK - 2 -4 1 0 3 2: KO - 0 -4 1 2 0 3: KO - 0 -4 1 2 3 0: OK - 3 -4 1 3 0 2: KO - 0 -4 1 3 2 0: KO - 0 -4 2 0 1 3: KO - 0 -4 2 0 3 1: KO - 0 -4 2 1 0 3: KO - 0 -4 2 1 3 0: KO - 0 -4 2 3 0 1: KO - 0 -4 2 3 1 0: KO - 0 -4 3 0 1 2: OK - 3 -4 3 0 2 1: KO - 0 -4 3 1 0 2: KO - 0 -4 3 1 2 0: KO - 0 -4 3 2 0 1: KO - 0 -4 3 2 1 0: KO - 0 -** stacks resulting in nb_moves > 7 : ** diff --git a/backup/sort.c b/backup/sort.c deleted file mode 100644 index df742cd..0000000 --- a/backup/sort.c +++ /dev/null @@ -1,127 +0,0 @@ -#include "push_swap.h" - -int nb_sorted_at_pos(int *stack, int size, int pos) -{ - int nb_sorted; - int curr_value; - int i; - - i = pos + 1; - nb_sorted = 1; - while (i != pos && !(pos == 0 && i == size)) - { - if (i == size) - curr_value = stack[0]; - else - curr_value = stack[i]; - if (i == 0) - { - i++; - continue ; - } - if (curr_value > stack[i - 1]) - nb_sorted++; - if (i == size) - i = 0; - else - i++; - } - return (nb_sorted); -} - -int get_biggest_val(int *stack, int size) -{ - int pos_in_stack; - int pos_biggest; - int biggest_val; - - pos_in_stack = 0; - pos_biggest = 0; - biggest_val = 0; - while (pos_in_stack < size) - { - if (stack[pos_in_stack] > biggest_val) - { - biggest_val = stack[pos_in_stack]; - pos_biggest = pos_in_stack; - } - pos_in_stack++; - } - return (biggest_val); -} - -int get_smallest_val(int *stack, int size) -{ - int pos_in_stack; - int smallest_pos; - int smallest_val; - - pos_in_stack = 0; - smallest_pos = 0; - smallest_val = size; - while (pos_in_stack < size) - { - if (stack[pos_in_stack] < smallest_val) - { - smallest_val = stack[pos_in_stack]; - smallest_pos = pos_in_stack; - } - pos_in_stack++; - } - return (smallest_val); -} - -int get_smallest_pos(int *stack, int size) -{ - int pos_in_stack; - int smallest_pos; - int smallest_val; - - pos_in_stack = 0; - smallest_pos = 0; - smallest_val = size; - while (pos_in_stack < size) - { - if (stack[pos_in_stack] < smallest_val) - { - smallest_val = stack[pos_in_stack]; - smallest_pos = pos_in_stack; - } - pos_in_stack++; - } - return (smallest_pos); -} - -void sort_3(t_main_cont *cont) -{ - int smallest_pos; - int nb_sorted; - - smallest_pos = get_smallest_pos(cont->A, cont->sizeA); - nb_sorted = nb_sorted_at_pos(cont->A, cont->sizeA, smallest_pos); - if (DEBUG) - printf("in sort_3, smallest_pos = %d\n", smallest_pos); - if (smallest_pos == 0 && nb_sorted == cont->size) - return ; - else if (nb_sorted == cont->size) - { - if (smallest_pos == 1) - make_rab(&cont->A, cont->sizeA, RA); - else if (smallest_pos == cont->size - 1) - make_rrab(&cont->A, cont->sizeA, RRA); - return ; - } - make_sab(cont->A, cont->sizeA, SA); - sort_3(cont); - return ; -} - -void sort(t_main_cont *cont) -{ - if (cont->size <= 3) - sort_3(cont); - - else if (cont->size <= 5) - sort_5(cont); - return ; -} diff --git a/backup/sort_5.c b/backup/sort_5.c deleted file mode 100644 index 8d139fd..0000000 --- a/backup/sort_5.c +++ /dev/null @@ -1,262 +0,0 @@ -#include "push_swap.h" - -void rotate_a_to_pos0(t_main_cont *cont, int pos) -{ - if (pos < cont->sizeA - pos) - while (pos-- > 0) - make_rab(stack, size, RA); - else - while (pos++ < cont->sizeA) - make_rrab(&cont->A, cont->size, RRA); - if (DEBUG) - { - printf(YELLOW"--------AFTER rotate_a_to_pos0--------\n"RESET_COL); - print_single_stack(cont->A, cont->sizeA); - } - return ; -} - -void rotate_a_to_pos0_dry_run(t_main_cont *cont, int pos) -{ - if (pos < cont->sizeA - pos) - while (pos-- > 0) - rotate(stack, size); - else - while (pos++ < cont->sizeA) - rev_rotate(&cont->A, cont->size); - return ; -} - -bool try_pb(t_main_cont *cont) -{ - int pos_in_stack; - int stack_head_value; - - if (DEBUG) - printf(RED"-----ENTERED try_pb!!!-----\n"RESET_COL); - smallest_val = get_smallest_val(cont->A, cont->sizeA); - if (nb_sorted_at_pos(cont->A, cont->sizeA, smallest_val)) - { - rotate_a_to_pos0(cont, smallest_val); - return (true); - } - pos_in_stack = 0; - stack_head_value = cont->A[0]; - while (pos_in_stack < cont->sizeA) - { - rotate_a_to_pos0_dry_run(cont, pos_in_stack); - push(cont, PB); - if (DEBUG) - { - printf(RED"------after pushing stackA[%d]------\n"RESET_COL, pos_in_stack); - // print_stacks(cont); - - } - if (try_sa_dry_run(cont)) - { - push(cont, PA); - rotate_a_to_pos0_dry_run(cont, get_pos_in_stack(cont->A, cont->sizeA, stack_head_value)); - rotate_a_to_pos0(cont, pos_in_stack); - make_push(cont, PB); - try_sa(cont); - insert_b(cont); - if (DEBUG) - { - printf(GREEN"--------try_pb succeeded--------\n"RESET_COL); - // print_stacks(cont); - } - return (true); - } - else - { - push(cont, PA); - rotate_to_pos0_dry_run(&cont->A, cont->sizeA, get_stack_pos(cont->A, cont->sizeA, stack_head_value)); - } - pos_in_stack++; - } - return (false); -} - -bool try_sa_dry_run(t_main_cont *cont) -{ - int *stack; - int size; - int pos; - int stack_head; - int next_pos; - - stack = cont->A; - size = cont->sizeA; - pos = 0; - while (pos < size) - { - next_pos = get_next_pos(pos, size); - ft_swap_ints(&stack[pos], &stack[next_pos]); - stack_head = get_pos_smallest(stack, size); - if (nb_sorted_at_pos(stack, size, stack_head) == size) - { - ft_swap_ints(&stack[pos], &stack[next_pos]); - return (true); - } - else - ft_swap_ints(&stack[pos], &stack[next_pos]); - pos++; - } - return (false); -} - -bool try_sa(t_main_cont *cont) -{ - int *stack; - int size; - int pos; - int stack_head; - int next_pos; - - stack = cont->A; - size = cont->sizeA; - pos = 0; - while (pos < size) - { - next_pos = get_next_pos(pos, size); - ft_swap_ints(&stack[pos], &stack[next_pos]); - stack_head = get_pos_smallest(stack, size); - if (nb_sorted_at_pos(stack, size, stack_head) == size) - { - ft_swap_ints(&stack[pos], &stack[next_pos]); - rotate_to_pos0(&cont->A, cont->sizeA, pos); - make_sab(cont->A, cont->sizeA, SA); - if (DEBUG) - { - printf(YELLOW"--------AFTER try_sa--------\n"RESET_COL); - // print_stacks(cont); - } - return (true); - } - else - ft_swap_ints(&stack[pos], &stack[next_pos]); - pos++; - } - return (false); -} - -int try_ra_dry_run(t_main_cont *cont) -{ - int stack_head; - - smallest_val = get_smallest_pos(cont->A, cont->size); - if (nb_sorted_at_pos(cont->A, cont->size, smallest_val) == cont->sizeA) - { - return (true); - } - return (false); -} - -bool try_ra(t_main_cont *cont) -{ - int stack_head; - - smallest_val = get_smallest_pos(cont->A, cont->size); - if (nb_sorted_at_pos(cont->A, cont->size, smallest_val) == cont->sizeA) - { - rotate_a_to_pos0(cont, smallest_val); - return (true); - } - return (false); -} - -void insert_b(t_main_cont *cont) -{ - int pos_of_b0_in_a; - int insertion_val; - - while (cont->sizeB) - { - if (cont->B[0] < cont->size - 1) - insertion_val = cont->B[0] + 1; - else - insertion_val = 0; - if (DEBUG) - printf(RED"------>in insert_b, insertion_val of %d = %d\n"RESET_COL, cont->B[0], insertion_val); - pos_of_b0_in_a = 0; - while (cont->A[pos_of_b0_in_a] != insertion_val) - { - set_next_pos(&pos_of_b0_in_a, cont->sizeA); - if (pos_of_b0_in_a == 0) - set_next_pos(&insertion_val, cont->size); - } - if (DEBUG) - printf(RED"------>in insert_b, pos_of %d in a = %d\n"RESET_COL, insertion_val, pos_of_b0_in_a); - rotate_to_pos0(&cont->A, cont->sizeA, pos_of_b0_in_a); - make_push(cont, PA); - if (DEBUG) - { - printf(YELLOW"--------AFTER pa--------\n"RESET_COL); - // print_stacks(cont); - } - } - insertion_val = get_pos_smallest(cont->A, cont->sizeA); - rotate_to_pos0(&cont->A, cont->sizeA, insertion_val); - return ; -} - -bool try_rotate_and_swap(t_main_cont *cont) -{ - int stack_head; - - stack_head = get_pos_smallest(cont->A, cont->sizeA); - if (nb_sorted_at_pos(cont->A, cont->sizeA, stack_head) == cont->sizeA) - { - insert_b(cont); - if (DEBUG) - { - printf(RED"#############stack_head = %d\n"RESET_COL, stack_head); - printf(RED"#############cont->A[stack_head] = %d\n"RESET_COL, cont->A[stack_head]); - } - return (true); - } - else if (try_sa(cont) == true) - { - insert_b(cont); - if (DEBUG) - { - printf(RED"#############stack_head = %d\n"RESET_COL, stack_head); - printf(RED"#############cont->A[stack_head] = %d\n"RESET_COL, cont->A[stack_head]); - } - return (true); - } - return (false); -} - -//Need to code try_pb (which will try ALL positions in stack A to see which -//is closest to pos_0) to try to see if pushing a single value can allow -//try_sa to sort the 4 remaining ones. -//I believe that the only case that would result in more than 7 moves with this -//method is "4 3 2 1 0" (8 moves). The only way I found to make it in less is : -// -pb; sa-> 2 3 1 0 | 4 -ra x2; sa-> 0 1 2 3 | 4 -pa; ra-> [DONE] (7 moves) - -void sort_5(t_main_cont *cont) -{ - if (try_rotate_and_swap(cont) == true) - { - if (DEBUG) - { - printf("--------SOLVED BOARD--------"); - // print_stacks(cont); - } - return ; - } - else - { - if (try_pb(cont) == true) - return ; - make_push(cont, PB); - if (DEBUG) - { - printf(YELLOW"--------AFTER pb--------\n"RESET_COL); - // print_stacks(cont); - } - sort_5(cont); - } - return ; -} diff --git a/backup/sort_big.c b/backup/sort_big.c deleted file mode 100644 index edf3af0..0000000 --- a/backup/sort_big.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "push_swap.h" - -void sort_big(t_main_cont *cont) -{ - int max_index_in_a; - - max_index_in_a -} \ No newline at end of file diff --git a/backup/try_pb.log b/backup/try_pb.log deleted file mode 100644 index e8a0c27..0000000 --- a/backup/try_pb.log +++ /dev/null @@ -1,245 +0,0 @@ -0 1 2 3 4: OK - 0 -0 1 2 4 3: OK - 5 -0 1 3 2 4: OK - 5 -0 1 3 4 2: OK - 4 -0 1 4 2 3: OK - 4 -0 1 4 3 2: OK - 8 -0 2 1 3 4: OK - 3 -0 2 1 4 3: OK - 7 -0 2 3 1 4: OK - 6 -0 2 3 4 1: OK - 2 -0 2 4 1 3: OK - 5 -0 2 4 3 1: OK - 6 -0 3 1 2 4: OK - 6 -0 3 1 4 2: OK - 5 -0 3 2 1 4: OK - 8 -0 3 2 4 1: OK - 4 -0 3 4 1 2: OK - 7 -0 3 4 2 1: OK - 5 -0 4 1 2 3: OK - 2 -0 4 1 3 2: OK - 6 -0 4 2 1 3: OK - 4 -0 4 2 3 1: OK - 5 -0 4 3 1 2: OK - 5 -0 4 3 2 1: OK - 7 -1 0 2 3 4: OK - 1 -1 0 2 4 3: OK - 7 -1 0 3 2 4: OK - 5 -1 0 3 4 2: OK - 6 -1 0 4 2 3: OK - 6 -1 0 4 3 2: OK - 8 -1 2 0 3 4: OK - 5 -1 2 0 4 3: OK - 7 -1 2 3 0 4: OK - 4 -1 2 3 4 0: OK - 1 -1 2 4 0 3: OK - 5 -1 2 4 3 0: OK - 5 -1 3 0 2 4: OK - 6 -1 3 0 4 2: OK - 7 -1 3 2 0 4: OK - 7 -1 3 2 4 0: OK - 4 -1 3 4 0 2: OK - 3 -1 3 4 2 0: OK - 7 -1 4 0 2 3: OK - 6 -1 4 0 3 2: OK - 6 -1 4 2 0 3: OK - 6 -1 4 2 3 0: OK - 7 -1 4 3 0 2: OK - 5 -1 4 3 2 0: OK - 7 -2 0 1 3 4: OK - 5 -2 0 1 4 3: OK - 7 -2 0 3 1 4: OK - 7 -2 0 3 4 1: OK - 8 -2 0 4 1 3: OK - 6 -2 0 4 3 1: OK - 6 -2 1 0 3 4: OK - 7 -2 1 0 4 3: OK - 9 -2 1 3 0 4: OK - 8 -2 1 3 4 0: OK - 2 -2 1 4 0 3: OK - 7 -2 1 4 3 0: OK - 6 -2 3 0 1 4: OK - 6 -2 3 0 4 1: OK - 4 -2 3 1 0 4: OK - 8 -2 3 1 4 0: OK - 6 -2 3 4 0 1: OK - 2 -2 3 4 1 0: OK - 3 -2 4 0 1 3: OK - 4 -2 4 0 3 1: OK - 8 -2 4 1 0 3: OK - 8 -2 4 1 3 0: OK - 7 -2 4 3 0 1: OK - 4 -2 4 3 1 0: OK - 6 -3 0 1 2 4: OK - 4 -3 0 1 4 2: OK - 8 -3 0 2 1 4: OK - 8 -3 0 2 4 1: OK - 7 -3 0 4 1 2: OK - 3 -3 0 4 2 1: OK - 5 -3 1 0 2 4: OK - 6 -3 1 0 4 2: OK - 7 -3 1 2 0 4: OK - 7 -3 1 2 4 0: OK - 6 -3 1 4 0 2: OK - 8 -3 1 4 2 0: OK - 7 -3 2 0 1 4: OK - 7 -3 2 0 4 1: OK - 6 -3 2 1 0 4: OK - 9 -3 2 1 4 0: OK - 7 -3 2 4 0 1: OK - 3 -3 2 4 1 0: OK - 8 -3 4 0 1 2: OK - 2 -3 4 0 2 1: OK - 4 -3 4 1 0 2: OK - 3 -3 4 1 2 0: OK - 6 -3 4 2 0 1: OK - 6 -3 4 2 1 0: OK - 9 -4 0 1 2 3: OK - 1 -4 0 1 3 2: OK - 5 -4 0 2 1 3: OK - 4 -4 0 2 3 1: OK - 5 -4 0 3 1 2: OK - 5 -4 0 3 2 1: OK - 9 -4 1 0 2 3: OK - 2 -4 1 0 3 2: OK - 6 -4 1 2 0 3: OK - 7 -4 1 2 3 0: OK - 3 -4 1 3 0 2: OK - 6 -4 1 3 2 0: OK - 7 -4 2 0 1 3: OK - 7 -4 2 0 3 1: OK - 6 -4 2 1 0 3: OK - 8 -4 2 1 3 0: OK - 5 -4 2 3 0 1: OK - 7 -4 2 3 1 0: OK - 6 -4 3 0 1 2: OK - 3 -4 3 0 2 1: OK - 7 -4 3 1 0 2: OK - 5 -4 3 1 2 0: OK - 6 -4 3 2 0 1: OK - 6 -4 3 2 1 0: OK - 8 -** stacks resulting in nb_moves > 8 : ** - 2 1 0 4 3 --> 9 - 3 2 1 0 4 --> 9 - 3 4 2 1 0 --> 9 - 4 0 3 2 1 --> 9 diff --git a/backup/vectors.c b/backup/vectors.c deleted file mode 100644 index b633f2a..0000000 --- a/backup/vectors.c +++ /dev/null @@ -1,86 +0,0 @@ -#include "push_swap.h" - -bool vector_init(t_vector *vector) -{ - vector->ptr_vec_add = vec_add; - vector->ptr_vec_remove = vec_remove; - vector->ptr_vec_resize = vec_resize; - vector->ptr_vec_free_list = vec_free_list; - - vector.nb_elems = 0; - vector.capacity = VECTOR_INIT_SIZE; - vector.elems = malloc(VECTOR_INIT_SIZE * sizeof(int *)); - if (!vector.elems) - return (FAILURE); - return (SUCCESS); -} - -bool vec_add(t_vector *vector, int new_elem) -{ - t_vec_list *list; - bool status; - - list = &vector; - if (list->nb_elems == list->capacity) - { - status = vec_resize(vector, list->capacity + list->capacity); - if (status == FAILURE) - return (status); - } - list->elems[list->nb_elems] = new_elem; - list->nb_elems += 1; - status = SUCCESS; - return (status); -} - -//Need to add resize smaller when under a certain size -void vec_remove(t_vector *vector) -{ - t_vec_list *list; - - list = &vector; - list->elems[list->nb_elems - 1] = 0; - list->nb_elems -= 1; - return ; -} - -bool vec_resize(t_vector *vector, int new_size) -{ - int *temp; - int pos_in_vector; - bool status; - - if (new_size < vector.capacity) - return (FAILURE); - if (DEBUG) - printf(RED"ENTERED RESIZE_END() WITH SIZE = %d\n", new_size); - //To switch to a bzero, or write a ft_calloc - temp = malloc(new_size * sizeof(int)); - if (DEBUG) - printf("SURVIVED MALLOC\n"RESET_COL); - if (!temp) - status = FAILURE; - else - { - ft_bzero(temp, new_size); - pos_in_vector = 0; - while (pos_in_vector < vector.nb_elems) - { - temp[pos_in_vector] = vector.elems[pos_in_vector]; - pos_in_vector++; - } - free (vector.elems); - vector.elems = temp; - if (DEBUG) - printf(RED"SURVIVED FREE()\n"RESET_COL); - vector.capacity = new_size; - status = SUCCESS; - } - return (status); -} - -bool vec_free_list(t_vector *vector) -{ - free (vector.elems); - return (FAILURE); -} diff --git a/greater_than_ideas.txt b/greater_than_ideas.txt deleted file mode 100644 index a7acd51..0000000 --- a/greater_than_ideas.txt +++ /dev/null @@ -1,140 +0,0 @@ -3 4 1 7 10 6 5 8 9 2 - ^ - HEAD -- - x x x - - - - - => 3 -- - x x - - - x x - => 4 - -3 4 1 7 10 6 5 8 9 2 - ^ - HEAD -x x - x x - - - - x => 5 -x x - x - - - x x x => 6 - -12 6 2 10 8 4 16 7 18 13 1 9 5 3 14 19 15 11 17 20 - ^ - HEAD -- - - - - - - - - - x x - - x x - - x x => 6 -- - - - - - - - - - x x - - x x - - x x => 6 -- - - - - - - - - - x x - - x - x - x x => 6 -- - - - - - - - - - x - x - x - x - x x => 6 -- - - - - - - - - - x - - x x - x - x x => 6 - -12 6 2 10 8 4 16 7 18 13 1 9 5 3 14 19 15 11 17 20 - ^ - HEAD -- - x x - - x - x - - - - - - x - - - x => 6 -- - x - - x - x - - - x - - x - x - x x => 8 -->so there are different ways to calculate... - - TAIL - v -15 8 20 12 13 17 1 7 4 3 14 18 10 11 2 6 16 19 9 5 - ^ - HEAD -- - x - - - x x - - x x - - - - - x - - => 6 - -1 7 4 3 14 18 10 11 2 6 16 19 9 5 15 8 20 12 13 17 -x x -----yes------> x --no->x --no---> x x x (range = 3) -x x----yes------> x --no->x --no---> x x x - -1 7 4 3 14 18 10 11 2 6 16 19 9 5 15 8 20 12 13 17 (range = 4) -x --x-----------------x-------x------------x-x--x - -12 19 17 7 3 18 1 8 5 6 14 10 16 13 11 15 4 9 20 2 - -1 8 5 6 14 10 16 13 11 15 4 9 20 2 12 19 17 7 3 18 -y y n y y n y n n y n y n n y y n n n y =>10 - -1 8 5 6 14 10 16 13 11 15 4 9 20 2 12 19 17 7 3 18 -x--------------------------------x------------x--- (range = 1) 3, best = 3 -x--------------------------------x------------x--- (range = 2) 3, best = 3 -x-------------------------x-----------------x----- (range = 3) 3, best = 3 -x---x-x----x-----x-----x--------------x----------- (range = 4) 7 - o->x pop the last one -x---x-x----x-----x-----x--------------o--x-----x- 8 -x<--ox pop the last one -x---x-x----x-----x-----x-------o------x----------- 7 -x---x-x----x-----x-----x-------o----->o--x pop the last one -x---x-x----x-----x-----x-------o------o--x------x- 8 -x<--o 12. -I believe this gives a good ballpark of theoretical max, but it almost always gonna be less than this - - -1. Map values to their positions ! This way you have a lookup table and don't have to iterate through values -e.g. -val_pos[1] : 6 -val_pos[2] : 14 -val_pos[3] : 9 - (...) -val_pos[19] : 17 -val_pos[20] : 2 -They could all be iterators, so that they're easy to commonly increment/decrement. Should not be done more than a max of nb_vals times anyways -could store a max_value_left variable to check if max_value_left - curr_value + nb_increasing > - -new stack: -15 20 13 17 1 7 14 18 11 6 16 19 ---->DOESN'T WORK, b/c you might want to exclude some of them, therefore including some that are not there rn - -temp result for 100 with up to 3 - - -with delta_values: -1. - Nb tests done: 1000 - Average nb of moves: 575 - Worst nb of moves: 639 - Nb of moves above 575 : 507 -2. - Nb tests done: 1000 - Average nb of moves: 576 - Worst nb of moves: 651 - Nb of moves above 575 : 524 - -without delta_values -1. - Nb tests done: 1000 - Average nb of moves: 580 - Worst nb of moves: 649 - Nb of moves above 575 : 567 -2. - Nb tests done: 1000 - Average nb of moves: 581 - Worst nb of moves: 657 - Nb of moves above 575 : 573 - - -5500: - Average nb of moves: 5155 - (Test #414) Worst nb of moves: 5534 - Nb of moves above 5304 : 125 - - Nb tests done: 5001 - Average nb of moves: 5169 - (Test #2538) Worst nb of moves: 5634 - Nb of moves above 5500 : 22 - - - -for now seems to reduce the nb of the max_moves, but could be an outlier \ No newline at end of file diff --git a/include/deques.h b/include/deques.h index a449384..c7fc8f6 100644 --- a/include/deques.h +++ b/include/deques.h @@ -1,9 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* deques.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- +# include # include # include +# include "../libft/libft.h" # define VECTOR_INIT_SIZE 16 # define SUCCESS 0 @@ -12,8 +26,8 @@ //VECTORS (need to learn how to properly link multiple headers in Makefile) //Learned structure from https://aticleworld.com/implement-vector-in-c/ //Decided to fuck it up of my own volition, though -typedef struct s_deque t_deque; -typedef struct s_deque +typedef struct s_deque t_deque; +struct s_deque { int *elems; int *malloced_space; @@ -23,42 +37,44 @@ typedef struct s_deque int capacity_total; int capacity_end; int capacity_front; - bool (*add_front)(t_deque *, int); - bool (*add_last)(t_deque *, int); void (*free_list)(t_deque *); int (*get_elem_max)(t_deque *); int (*get_elem_min)(t_deque *); - bool (*reinit_list)(t_deque *); + void (*reinit_list)(t_deque *); void (*remove_front)(t_deque *); void (*remove_last)(t_deque *); - bool (*resize_end)(t_deque *, int); - bool (*resize_front)(t_deque *, int); + void (*add_front)(t_deque *, int); + void (*add_last)(t_deque *, int); + void (*resize_end)(t_deque *, int); + void (*resize_front)(t_deque *, int); void (*set_elem)(t_deque *, int, int); -} t_deque; +}; + +// deques.c +void init_deque(t_deque *deque); +void deque_reinit_list(t_deque *deque); -// Deques -void init_deque(t_deque *deque); -bool deque_add_front(t_deque *deque, int new_elem); -bool deque_add_last(t_deque *deque, int new_elem); -void deque_free_list(t_deque *deque); +// deque_utils.c +void deque_resize_end(t_deque *deque, int new_size); +void deque_resize_front(t_deque *deque, int new_size); +void deque_free_list(t_deque *deque); int deque_get_elem_max(t_deque *deque); int deque_get_elem_min(t_deque *deque); -bool deque_reinit_list(t_deque *deque); + +// deque_modify.c +void deque_add_front(t_deque *deque, int new_elem); +void deque_add_last(t_deque *deque, int new_elem); void deque_remove_front(t_deque *deque); void deque_remove_last(t_deque *deque); -bool deque_resize_end(t_deque *deque, int new_size); -bool deque_resize_front(t_deque *deque, int new_size); void deque_set_elem(t_deque *deque, int pos, int new_value); -// Operators +// deque_operators.c void new_deque(t_deque **deque); t_deque *clone_deque(t_deque *src); void copy_deque(t_deque *src, t_deque *dest); void cat_deque(t_deque *src, t_deque *dest); void cat_deque_front(t_deque *src, t_deque *dest); - -//DUMB SHIT TO REMOVE //ERROR HANDLING void exit_on_err(char *err_message); void ft_bzero(void *s, size_t n); diff --git a/include/iterators.h b/include/iterators.h index 12b94ec..21dc194 100644 --- a/include/iterators.h +++ b/include/iterators.h @@ -1,9 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iterators.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- - //ITERATORS (still need to learn how to link headers...) # define REVERSE 1 # define CANONICAL 0 diff --git a/include/moves.h b/include/moves.h index d3b6698..9bc97b2 100644 --- a/include/moves.h +++ b/include/moves.h @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* moves.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- size) + { + if (block_ids->elems[i] == id) + return (true); + i++; + } + return (false); +} + +void assign_block_median( + t_deque *stack, t_deque *block_ids, t_block_info *info) +{ + int curr_pos; + + info->median_val = 0; + curr_pos = 0; + while (curr_pos < stack->size) + { + if (block_ids->elems[curr_pos] == info->curr_block_id) + { + if (stack->elems[curr_pos] < info->min_val) + info->min_val = stack->elems[curr_pos]; + else if (stack->elems[curr_pos] > info->max_val) + info->max_val = stack->elems[curr_pos]; + } + curr_pos++; + } + info->median_val = (info->max_val - info->min_val) / 2 + info->min_val; + return ; +} + +bool has_smaller_than_median(t_deque *stack, t_block_info *info) +{ + int curr_pos; + + curr_pos = 0; + while (curr_pos < stack->size) + { + if (stack->elems[curr_pos] >= info->min_val + && stack->elems[curr_pos] <= info->median_val) + return (true); + curr_pos++; + } + return (false); +} + +void rotate_block_split( + t_main_cont *cont, t_deque *block_ids, t_block_info *info) +{ + int pos_a; + + pos_a = 0; + while (block_ids->elems[pos_a] != info->curr_block_id) + pos_a++; + if (has_smaller_than_median(&cont->stack_b, info)) + { + while (pos_a && cont->stack_b.elems[0] > info->median_val + && cont->stack_b.elems[0] <= info->max_val) + { + do_rr(cont, &cont->curr_moves); + block_ids->add_last(block_ids, block_ids->elems[0]); + block_ids->remove_front(block_ids); + pos_a--; + } + } + while (pos_a) + { + do_ra(cont, &cont->curr_moves); + block_ids->add_last(block_ids, block_ids->elems[0]); + block_ids->remove_front(block_ids); + pos_a--; + } + return ; +} diff --git a/src/calc_insert_cost.c b/src/calc_insert_cost.c index 3b98d38..0ce736a 100644 --- a/src/calc_insert_cost.c +++ b/src/calc_insert_cost.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* calc_insert_cost.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- dist0 = a_info->pos; if (ft_abs(a_info->revpos) < a_info->pos) a_info->dist0 = a_info->revpos; - b_info->dist0 = b_info->pos; if (ft_abs(b_info->revpos) < b_info->pos) b_info->dist0 = b_info->revpos; - - //If equal in both directions, pick the one in the same direction as the other - if (ft_abs(a_info->revpos) == a_info->pos && b_info->dist0 < 0) a_info->dist0 = a_info->revpos; if (ft_abs(b_info->revpos) == b_info->pos && a_info->dist0 < 0) @@ -32,7 +40,7 @@ static int get_best_absolute_cost( ft_max(a_info->pos, b_info->pos), ft_max(-a_info->revpos, -b_info->revpos)); curr_cost = ft_min( - curr_cost, + curr_cost, ft_abs(a_info->dist0) + ft_abs(b_info->dist0)); return (curr_cost); } @@ -44,7 +52,7 @@ static int get_best_absolute_cost( // int calc_insert_cost(t_insert_info *info) { - int curr_cost; + int curr_cost; set_best_individual_costs(&info->a_info, &info->b_info); curr_cost = get_best_absolute_cost(&info->a_info, &info->b_info); @@ -68,8 +76,8 @@ static bool is_new_best_moves(t_insert_info *info) void update_insert_info(t_main_cont *cont, t_insert_info *info) { - t_stack_insert_info *a_info; - t_stack_insert_info *b_info; + t_stack_insert_info *a_info; + t_stack_insert_info *b_info; a_info = &info->a_info; b_info = &info->b_info; @@ -93,5 +101,4 @@ void update_insert_info(t_main_cont *cont, t_insert_info *info) info->min_cost = info->curr_cost; info->min_delta_insert = info->curr_delta_insert; } - return ; } diff --git a/src/checker_bonus.c b/src/checker_bonus.c new file mode 100644 index 0000000..02af877 --- /dev/null +++ b/src/checker_bonus.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* checker_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- moves_fcts[convert_move_to_index(move)](checker); + free(curr_line); + curr_line = read_line(STDIN_FILENO); + } + return ; +} + +int main(int argc, char *argv[]) +{ + t_checker *checker; + int i; + + if (argc < 2) + exit(EXIT_SUCCESS); + checker = ft_xalloc(sizeof(t_checker)); + parse_bonus(checker, argc, argv); + do_moves(checker); + i = 0; + while (++i < checker->stack_a.size) + { + if (checker->stack_a.elems[i] < checker->stack_a.elems[i - 1]) + { + write(STDOUT_FILENO, "KO\n", 3); + free_checker(checker); + return (0); + } + } + write(STDOUT_FILENO, "OK\n", 3); + free_checker(checker); + return (0); +} diff --git a/src/checker_utils_bonus.c b/src/checker_utils_bonus.c new file mode 100644 index 0000000..c2e8a4c --- /dev/null +++ b/src/checker_utils_bonus.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* checker_utils_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- stack_a); + init_deque(&checker->stack_b); + checker->moves_fcts = ft_xalloc(11 * sizeof(void *)); + i = -1; + while (args[++i] != NULL) + checker->stack_a.add_last(&checker->stack_a, ft_atoi(args[i])); + set_moves_pointers(checker->moves_fcts); + return ; +} + +void bonus_exit_on_err(t_checker *checker, char *curr_line) +{ + free(curr_line); + close(STDIN_FILENO); + read_line(STDIN_FILENO); + free_checker(checker); + ft_putstr_fd("Error\n", STDERR_FILENO); + exit (255); +} + +void free_checker(t_checker *checker) +{ + checker->stack_a.free_list(&checker->stack_a); + checker->stack_b.free_list(&checker->stack_b); + free(checker->moves_fcts); + free(checker); + return ; +} diff --git a/src/cherrypick_utils.c b/src/cherrypick_utils.c deleted file mode 100644 index c423cf0..0000000 --- a/src/cherrypick_utils.c +++ /dev/null @@ -1,29 +0,0 @@ -#include "push_swap.h" - -bool is_in_stack(t_deque *stack, int val) -{ - int i; - i = 0; - while (i < stack->size) - { - if (stack->elems[i] == val) - return (true); - i++; - } - return (false); -} - -void free_deque_list(t_deque **list, int size) -{ - int i; - - i = 0; - while (i < size) - { - list[i]->free_list(list[i]); - free (list[i]); - i++; - } - free (list); - return ; -} diff --git a/src/cleanup.c b/src/cleanup.c index d778f24..95682a6 100644 --- a/src/cleanup.c +++ b/src/cleanup.c @@ -1,10 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cleanup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- stack_a.free_list(&cont->stack_a); cont->stack_b.free_list(&cont->stack_b); - // cont->final_moves.free_list(&cont->final_moves); cont->curr_moves.free_list(&cont->curr_moves); cont->best_moves.free_list(&cont->best_moves); cont->best_moves.free_list(&cont->initial_stack); diff --git a/src/delta_insert.c b/src/delta_insert.c new file mode 100644 index 0000000..fb846dd --- /dev/null +++ b/src/delta_insert.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* delta_insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- stack_a.max_elem; + min_a = cont->stack_a.min_elem; + max_b = cont->stack_b.max_elem; + min_b = cont->stack_b.min_elem; + delta_insert = ft_abs(val_a - val_b); + if (delta_insert <= (ft_max(max_a, max_b) + 1) / 2) + return (delta_insert); + delta_insert = (max_a - val_a) + (val_b - min_b) + 1; + if (val_b > val_a) + delta_insert = (max_b - val_b) + (val_a - min_a) + 1; + return (delta_insert); +} + +int calc_delta_insert(t_main_cont *cont, t_insert_info *info) +{ + return (calc_delta_two_values(cont, info->a_info.val, info->b_info.val)); +} diff --git a/src/deque_modify.c b/src/deque_modify.c new file mode 100644 index 0000000..aec1bb3 --- /dev/null +++ b/src/deque_modify.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* deque_modify.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- capacity_front == 0) + deque_resize_front(deque, deque->capacity_total * 2); + deque->elems -= 1; + deque->elems[0] = new_elem; + deque->size += 1; + deque->capacity_front -= 1; + deque->capacity_end += 1; + if (new_elem > deque->max_elem) + deque->max_elem = new_elem; + if (new_elem < deque->min_elem) + deque->min_elem = new_elem; + return ; +} + +void deque_add_last(t_deque *deque, int new_elem) +{ + if (deque->size == deque->capacity_end) + deque_resize_end(deque, deque->capacity_total * 2); + deque->elems[deque->size] = new_elem; + deque->size += 1; + if (new_elem > deque->max_elem) + deque->max_elem = new_elem; + if (new_elem < deque->min_elem) + deque->min_elem = new_elem; + return ; +} + +//Need to add resize smaller when under a certain size +void deque_remove_front(t_deque *deque) +{ + int elem_to_be_removed; + + elem_to_be_removed = deque->elems[0]; + deque->elems[0] = 0; + deque->elems += 1; + deque->capacity_front += 1; + deque->capacity_end -= 1; + deque->size -= 1; + if (elem_to_be_removed == deque->min_elem) + deque->min_elem = deque->get_elem_min(deque); + if (elem_to_be_removed == deque->max_elem) + deque->max_elem = deque->get_elem_max(deque); + return ; +} + +//Need to add resize smaller when under a certain size +void deque_remove_last(t_deque *deque) +{ + int elem_to_be_removed; + + elem_to_be_removed = deque->elems[deque->size - 1]; + deque->elems[deque->size - 1] = 0; + deque->size -= 1; + if (elem_to_be_removed == deque->min_elem) + deque->min_elem = deque->get_elem_min(deque); + if (elem_to_be_removed == deque->max_elem) + deque->max_elem = deque->get_elem_max(deque); + return ; +} + +void deque_set_elem(t_deque *deque, int pos, int new_value) +{ + deque->elems[pos] = new_value; + return ; +} diff --git a/src/deque_operators.c b/src/deque_operators.c index e6e84d0..93d011b 100644 --- a/src/deque_operators.c +++ b/src/deque_operators.c @@ -1,10 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* deque_operators.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- reinit_list(dest); i = 0; while (i < src->size) @@ -46,7 +56,7 @@ void cat_deque(t_deque *src, t_deque *dest) int i; if (!src || !dest) - return ; + return ; i = 0; while (i < src->size) { @@ -61,7 +71,7 @@ void cat_deque_front(t_deque *src, t_deque *dest) int i; if (!src || !dest) - return ; + return ; i = src->size - 1; while (i >= 0) { diff --git a/src/deque_utils.c b/src/deque_utils.c new file mode 100644 index 0000000..ad7356c --- /dev/null +++ b/src/deque_utils.c @@ -0,0 +1,98 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* deque_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- malloced_space); + deque->malloced_space = NULL; + return ; +} + +void deque_resize_front(t_deque *deque, int new_size) +{ + int *temp; + int total_new_size; + int pos_in_deque; + + total_new_size = new_size + deque->capacity_end; + temp = ft_xalloc(total_new_size * sizeof(int)); + temp += new_size; + pos_in_deque = 0; + while (pos_in_deque < deque->size) + { + temp[pos_in_deque] = deque->elems[pos_in_deque]; + pos_in_deque++; + } + free(deque->malloced_space); + deque->malloced_space = temp - new_size; + deque->elems = temp; + deque->capacity_front = new_size; + deque->capacity_total = total_new_size; + return ; +} + +void deque_resize_end(t_deque *deque, int new_size) +{ + int *temp; + int total_new_size; + int pos_in_deque; + + total_new_size = new_size + deque->capacity_front; + temp = ft_xalloc(total_new_size * sizeof(int)); + temp += deque->capacity_front; + pos_in_deque = 0; + while (pos_in_deque < deque->size) + { + temp[pos_in_deque] = deque->elems[pos_in_deque]; + pos_in_deque++; + } + free(deque->malloced_space); + deque->malloced_space = temp - deque->capacity_front; + deque->elems = temp; + deque->capacity_end = new_size; + deque->capacity_total = total_new_size; + return ; +} + +int deque_get_elem_max(t_deque *deque) +{ + int pos_in_stack; + int biggest_value; + + biggest_value = INT_MIN; + pos_in_stack = 0; + while (pos_in_stack < deque->size) + { + if (deque->elems[pos_in_stack] > biggest_value) + biggest_value = deque->elems[pos_in_stack]; + pos_in_stack++; + } + return (biggest_value); +} + +int deque_get_elem_min(t_deque *deque) +{ + int pos_in_stack; + int smallest_value; + + smallest_value = INT_MAX; + pos_in_stack = 0; + while (pos_in_stack < deque->size) + { + if (deque->elems[pos_in_stack] < smallest_value) + smallest_value = deque->elems[pos_in_stack]; + pos_in_stack++; + } + return (smallest_value); +} diff --git a/src/deques.c b/src/deques.c index a6c5720..61f57bc 100644 --- a/src/deques.c +++ b/src/deques.c @@ -1,224 +1,51 @@ -#include "deques.h" -#include - -void init_deque(t_deque *deque) -{ - //Add the atrocious list of helper functions(probs too much) - deque->add_front = &deque_add_front; - deque->add_last = &deque_add_last; - deque->free_list = &deque_free_list; - deque->get_elem_max = &deque_get_elem_max; - deque->get_elem_min = &deque_get_elem_min; - deque->reinit_list = &deque_reinit_list; - deque->remove_front = &deque_remove_front; - deque->remove_last = &deque_remove_last; - deque->resize_end = &deque_resize_end; - deque->resize_front = &deque_resize_front; - deque->set_elem = &deque_set_elem; - - - //Malloc a block of memory designed to leave space on each side of the - //deque_head, so that values can be added on both ends with the same - //calculation cost, while still sharing a single memory space. - deque->size = 0; - deque->min_elem = deque->get_elem_min(deque); - deque->max_elem = deque->get_elem_max(deque); - deque->capacity_end = VECTOR_INIT_SIZE; - deque->capacity_front = VECTOR_INIT_SIZE; - deque->capacity_total = 2 * VECTOR_INIT_SIZE; - deque->malloced_space = malloc(2 * VECTOR_INIT_SIZE * sizeof(int)); - if (!deque->malloced_space) - exit_on_err("init_deque error\n"); - ft_bzero(deque->malloced_space, 2 * VECTOR_INIT_SIZE); - deque->elems = &(deque->malloced_space[VECTOR_INIT_SIZE]); - return ; -} - -bool deque_add_front(t_deque *deque, int new_elem) -{ - bool status; - - if (deque->capacity_front == 0) - { - status = deque_resize_front(deque, deque->capacity_total); - if (status == FAILURE) - return (status); - } - deque->elems -= 1; - deque->elems[0] = new_elem; - deque->size += 1; - deque->capacity_front -= 1; - deque->capacity_end += 1; - if (new_elem > deque->max_elem) - deque->max_elem = new_elem; - if (new_elem < deque->min_elem) - deque->min_elem = new_elem; - status = SUCCESS; - return (status); -} - -bool deque_add_last(t_deque *deque, int new_elem) -{ - bool status; - - if (deque->size == deque->capacity_end) - { - status = deque_resize_end(deque, deque->capacity_total * 2); - if (status == FAILURE) - return (status); - } - deque->elems[deque->size] = new_elem; - deque->size += 1; - if (new_elem > deque->max_elem) - deque->max_elem = new_elem; - if (new_elem < deque->min_elem) - deque->min_elem = new_elem; - status = SUCCESS; - return (status); -} - -void deque_free_list(t_deque *deque) -{ - free (deque->malloced_space); - deque->malloced_space = NULL; - return ; -} - -int deque_get_elem_max(t_deque *deque) -{ - int pos_in_stack; - int biggest_value; +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* deques.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- size) - { - if (deque->elems[pos_in_stack] > biggest_value) - biggest_value = deque->elems[pos_in_stack]; - pos_in_stack++; - } - return (biggest_value); -} - -int deque_get_elem_min(t_deque *deque) -{ - int pos_in_stack; - int smallest_value; - - smallest_value = INT_MAX; - pos_in_stack = 0; - while (pos_in_stack < deque->size) - { - if (deque->elems[pos_in_stack] < smallest_value) - smallest_value = deque->elems[pos_in_stack]; - pos_in_stack++; - } - return (smallest_value); -} - -bool deque_reinit_list(t_deque *deque) -{ - free(deque->malloced_space); - deque->size = 0; - deque->min_elem = INT_MAX - 100; - deque->max_elem = INT_MIN + 100; - deque->capacity_end = VECTOR_INIT_SIZE; - deque->capacity_front = VECTOR_INIT_SIZE; - deque->capacity_total = 2 * VECTOR_INIT_SIZE; - deque->malloced_space = malloc(2 * VECTOR_INIT_SIZE * sizeof(int)); - if (!deque->malloced_space) - return (FAILURE); - ft_bzero(deque->malloced_space, 2 * VECTOR_INIT_SIZE); - //MADE A CHANGE HERE BY REMOVING THE ADDRESS ASSIGNATION - deque->elems = deque->malloced_space + VECTOR_INIT_SIZE; - return (SUCCESS); -} - -//Need to add resize smaller when under a certain size -void deque_remove_front(t_deque *deque) -{ - int elem_to_be_removed; - - elem_to_be_removed = deque->elems[0]; - deque->elems[0] = 0; - deque->elems += 1; - deque->capacity_front += 1; - deque->capacity_end -= 1; - deque->size -= 1; - if (elem_to_be_removed == deque->min_elem) - deque->min_elem = deque->get_elem_min(deque); - if (elem_to_be_removed == deque->max_elem) - deque->max_elem = deque->get_elem_max(deque); - return ; -} - -//Need to add resize smaller when under a certain size -void deque_remove_last(t_deque *deque) -{ - int elem_to_be_removed; +#include "deques.h" - elem_to_be_removed = deque->elems[deque->size - 1]; - deque->elems[deque->size - 1] = 0; - deque->size -= 1; - if (elem_to_be_removed == deque->min_elem) - deque->min_elem = deque->get_elem_min(deque); - if (elem_to_be_removed == deque->max_elem) - deque->max_elem = deque->get_elem_max(deque); +void init_deque(t_deque *deque) +{ + deque->add_front = &deque_add_front; + deque->add_last = &deque_add_last; + deque->free_list = &deque_free_list; + deque->get_elem_max = &deque_get_elem_max; + deque->get_elem_min = &deque_get_elem_min; + deque->reinit_list = &deque_reinit_list; + deque->remove_front = &deque_remove_front; + deque->remove_last = &deque_remove_last; + deque->resize_end = &deque_resize_end; + deque->resize_front = &deque_resize_front; + deque->set_elem = &deque_set_elem; + deque->size = 0; + deque->min_elem = deque->get_elem_min(deque); + deque->max_elem = deque->get_elem_max(deque); + deque->capacity_end = VECTOR_INIT_SIZE; + deque->capacity_front = VECTOR_INIT_SIZE; + deque->capacity_total = 2 * VECTOR_INIT_SIZE; + deque->malloced_space = ft_xalloc(2 * VECTOR_INIT_SIZE * sizeof(int)); + deque->elems = &(deque->malloced_space[VECTOR_INIT_SIZE]); return ; } -bool deque_resize_front(t_deque *deque, int new_size) +void deque_reinit_list(t_deque *deque) { - int *temp; - int total_new_size; - int pos_in_deque; - - total_new_size = new_size + deque->capacity_end; - temp = malloc(total_new_size * sizeof(int)); - if (!temp) - return (FAILURE); - temp += new_size; - pos_in_deque = 0; - while (pos_in_deque < deque->size) - { - temp[pos_in_deque] = deque->elems[pos_in_deque]; - pos_in_deque++; - } free(deque->malloced_space); - deque->malloced_space = temp - new_size; - deque->elems = temp; - deque->capacity_front = new_size; - deque->capacity_total = total_new_size; - return (SUCCESS); -} - -bool deque_resize_end(t_deque *deque, int new_size) -{ - int *temp; - int total_new_size; - int pos_in_deque; - - total_new_size = new_size + deque->capacity_front; - temp = malloc(total_new_size * sizeof(int)); - if (!temp) - return (FAILURE); - temp += deque->capacity_front; - pos_in_deque = 0; - while (pos_in_deque < deque->size) - { - temp[pos_in_deque] = deque->elems[pos_in_deque]; - pos_in_deque++; - } - free(deque->malloced_space); - deque->malloced_space = temp - deque->capacity_front; - deque->elems = temp; - deque->capacity_end = new_size; - deque->capacity_total = total_new_size; - return (SUCCESS); -} - -void deque_set_elem(t_deque *deque, int pos, int new_value) -{ - deque->elems[pos] = new_value; + deque->size = 0; + deque->min_elem = INT_MAX - 100; + deque->max_elem = INT_MIN + 100; + deque->capacity_end = VECTOR_INIT_SIZE; + deque->capacity_front = VECTOR_INIT_SIZE; + deque->capacity_total = 2 * VECTOR_INIT_SIZE; + deque->malloced_space = ft_xalloc(2 * VECTOR_INIT_SIZE * sizeof(int)); + deque->elems = deque->malloced_space + VECTOR_INIT_SIZE; return ; } diff --git a/src/errors.c b/src/errors.c index 6763bac..93dca65 100644 --- a/src/errors.c +++ b/src/errors.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* errors.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- mem_space); + info->buff = NULL; + info->mem_space = NULL; +} + +char *read_line(int fd) +{ + static t_read_info info; + + info.line = NULL; + if (!info.buff) + { + info.mem_space = ft_xalloc(BUFFSIZE); + info.buff = info.mem_space; + info.nb_bytes_read = read(fd, info.buff, BUFFSIZE); + } + else + info.nb_bytes_read = ft_strlen(info.buff); + if (info.nb_bytes_read > 0) + { + info.line_len = strlen_c(info.buff, '\n'); + if (info.line_len <= 0) + info.line_len = ft_strlen(info.buff); + info.line = ft_xalloc(info.line_len * sizeof(char)); + info.line = ft_strncpy(info.line, info.buff, info.line_len); + info.buff += info.line_len; + if (info.line_len == info.nb_bytes_read) + free_read_vars(&info); + return (info.line); + } + free_read_vars(&info); + return (NULL); +} diff --git a/src/ignore_LIS.c b/src/ignore_LIS.c index d8d69dc..25a86a3 100644 --- a/src/ignore_LIS.c +++ b/src/ignore_LIS.c @@ -1,109 +1,69 @@ -#include "push_swap.h" - -static void complete_ignore_lis_algo(t_main_cont *cont) -{ - insert_last_block(cont, &cont->curr_moves); - insert_b(cont, &cont->curr_moves); - rotate_to_0_in_a(cont, &cont->curr_moves, cont->head_a.index); - if (cont->curr_moves.size < cont->best_moves.size) - { - copy_deque(&cont->curr_moves, &cont->best_moves); - } - copy_deque(&cont->initial_stack, &cont->stack_a); - deque_reinit_list(&cont->stack_b); - deque_reinit_list(&cont->curr_moves); - return ; -} - - -void try_cutoff_n(t_main_cont *cont, t_deque *block_ids, int n, int cutoff) -{ - int i; - - i = 0; - while (i < n) - { - partition_stack_cutoff(&cont->stack_a, block_ids, cutoff); - insert_block_of_a_in_b(cont, block_ids, 1); - i++; - } - complete_ignore_lis_algo(cont); - return ; -} - -void try_n_divisions(t_main_cont *cont, t_deque *block_ids, int n) -{ - int i; - - i = 0; - while (i < n) - { - partition_stack_in_two(&cont->stack_a, block_ids); - insert_block_of_a_in_b(cont, block_ids, 1); - i++; - } - complete_ignore_lis_algo(cont); - return ; -} - -void try_n_divisions_smallest(t_main_cont *cont, t_deque *block_ids, int n) -{ - int i; +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ignore_LIS.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- stack_a, block_ids); - insert_block_of_a_in_b(cont, block_ids, 0); - i++; - } - complete_ignore_lis_algo(cont); - return ; -} +#include "push_swap.h" -void try_n_divisions_closest(t_main_cont *cont, t_deque *block_ids, int n) +static void ignore_lis2(t_main_cont *cont, t_deque *block_ids) { int i; - i = 0; - while (i < n) - { - partition_stack_in_two(&cont->stack_a, block_ids); - insert_block_of_a_in_b(cont, block_ids, block_ids->elems[0]); - i++; - } - complete_ignore_lis_algo(cont); - return ; + i = -1; + while (++i < 7) + try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 10 / 24); + i = -1; + while (++i < 7) + try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 11 / 24); + i = -1; + while (++i < 7) + try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 13 / 24); + i = -1; + while (++i < 7) + try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 14 / 24); + i = -1; + while (++i < 7) + try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 15 / 24); + i = -1; + while (++i < 7) + try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 16 / 24); + i = -1; + while (++i < 7) + try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 17 / 24); } void ignore_lis(t_main_cont *cont) { t_deque *block_ids; - int div_depth = 7; int i; new_deque(&block_ids); i = -1; - while (++i < div_depth) + while (++i < 7) try_n_divisions_smallest(cont, block_ids, i); i = -1; - while (++i < div_depth) + while (++i < 7) try_n_divisions_closest(cont, block_ids, i); i = -1; - while (++i < div_depth) + while (++i < 7) try_n_divisions(cont, block_ids, i); i = -1; - while (++i < div_depth) - try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 11 / 24); + while (++i < 7) + try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 7 / 24); i = -1; - while (++i < div_depth) - try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 10 / 24); + while (++i < 7) + try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 8 / 24); i = -1; - while (++i < div_depth) + while (++i < 7) try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 9 / 24); - i = -1; - while (++i < div_depth) - try_cutoff_n(cont, block_ids, i, cont->stack_a.max_elem * 7 / 24); + ignore_lis2(cont, block_ids); block_ids->free_list(block_ids); free(block_ids); } diff --git a/src/ignore_lis_utils.c b/src/ignore_lis_utils.c new file mode 100644 index 0000000..7cd2871 --- /dev/null +++ b/src/ignore_lis_utils.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ignore_lis_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- curr_moves); + insert_b(cont, &cont->curr_moves); + rotate_to_0_in_a(cont, &cont->curr_moves, cont->head_a.index); + if (cont->curr_moves.size < cont->best_moves.size) + { + copy_deque(&cont->curr_moves, &cont->best_moves); + } + copy_deque(&cont->initial_stack, &cont->stack_a); + deque_reinit_list(&cont->stack_b); + deque_reinit_list(&cont->curr_moves); + return ; +} + +void try_cutoff_n(t_main_cont *cont, t_deque *block_ids, int n, int cutoff) +{ + int i; + + i = 0; + while (i < n) + { + partition_stack_cutoff(&cont->stack_a, block_ids, cutoff); + insert_block_of_a_in_b(cont, block_ids, 1); + i++; + } + complete_algo(cont); + return ; +} + +void try_n_divisions(t_main_cont *cont, t_deque *block_ids, int n) +{ + int i; + + i = 0; + while (i < n) + { + partition_stack_in_two(&cont->stack_a, block_ids); + insert_block_of_a_in_b(cont, block_ids, 1); + i++; + } + complete_algo(cont); + return ; +} + +void try_n_divisions_smallest(t_main_cont *cont, t_deque *block_ids, int n) +{ + int i; + + i = 0; + while (i < n) + { + partition_stack_in_two(&cont->stack_a, block_ids); + insert_block_of_a_in_b(cont, block_ids, 0); + i++; + } + complete_algo(cont); + return ; +} + +void try_n_divisions_closest(t_main_cont *cont, t_deque *block_ids, int n) +{ + int i; + + i = 0; + while (i < n) + { + partition_stack_in_two(&cont->stack_a, block_ids); + insert_block_of_a_in_b(cont, block_ids, block_ids->elems[0]); + i++; + } + complete_algo(cont); + return ; +} diff --git a/src/init.c b/src/init.c index 289e5a7..77145b5 100644 --- a/src/init.c +++ b/src/init.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- initial_stack); init_deque(&cont->staying_vals); init_deque(&cont->leaving_vals); - cont->reverse_fcts = ft_safealloc(11 * sizeof(void *)); + cont->reverse_fcts = ft_xalloc(11 * sizeof(void *)); + init_reverse_moves_array(cont->reverse_fcts); i = -1; while (args[++i] != NULL) - if (cont->stack_a.add_last(&cont->stack_a, ft_atoi(args[i]))) - exit_on_err("deque_add_last done fucked up\n"); - + cont->stack_a.add_last(&cont->stack_a, ft_atoi(args[i])); normalize_stack_values(&cont->stack_a); copy_deque(&cont->stack_a, &cont->initial_stack); - set_iterator(&cont->head_a, get_pos_smallest_val(&cont->stack_a), - cont->stack_a.size, 0); + cont->stack_a.size, 0); set_iterator(&cont->head_b, 0, 0, 1); - init_reverse_moves_array(cont->reverse_fcts); cont->min_nb_moves = INT_MAX; return ; } void init_reverse_moves_array(void (**array)(t_main_cont *, t_deque *)) { - array[REV_SA] = &do_sa; - array[REV_SB] = &do_sb; - array[REV_SS] = &do_ss; - array[REV_RA] = &do_rra; - array[REV_RB] = &do_rrb; - array[REV_RR] = &do_rrr; - array[REV_RRA] = &do_ra; - array[REV_RRB] = &do_rb; - array[REV_RRR] = &do_rr; - array[REV_PA] = &do_pb; - array[REV_PB] = &do_pa; + array[SA_INDEX] = &do_sa; + array[SB_INDEX] = &do_sb; + array[SS_INDEX] = &do_ss; + array[RA_INDEX] = &do_rra; + array[RB_INDEX] = &do_rrb; + array[RR_INDEX] = &do_rrr; + array[RRA_INDEX] = &do_ra; + array[RRB_INDEX] = &do_rb; + array[RRR_INDEX] = &do_rr; + array[PA_INDEX] = &do_pb; + array[PB_INDEX] = &do_pa; return ; } @@ -52,7 +61,7 @@ void normalize_stack_values(t_deque *stack) int pos_in_stack; int i; - normalized_stack = ft_safealloc(stack->size * sizeof(int)); + normalized_stack = ft_xalloc(stack->size * sizeof(int)); pos_in_stack = 0; while (pos_in_stack < stack->size) { @@ -72,5 +81,4 @@ void normalize_stack_values(t_deque *stack) i++; } free(normalized_stack); - return ; } diff --git a/src/insert.c b/src/insert.c index 83b4faf..1df7122 100644 --- a/src/insert.c +++ b/src/insert.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- stack_b.size <= 0) return ; - info = ft_safealloc(sizeof(t_insert_info)); - // if b is not empty, init will also call update_insert_info() - // init_insert_info(cont, info); + info = ft_xalloc(sizeof(t_insert_info)); info->b_info.pos = 0; info->min_cost = 1000; info->min_delta_insert = 1000; - while (info->b_info.pos < cont->stack_b.size) { update_insert_info(cont, info); - // Attempt to not try EVERY value, but only those that can result in fewer moves - // if stack_b pos is further than min_moves, set pos to stack_b.size - min_cost - if (info->b_info.pos > info->min_cost && - info->b_info.pos <= cont->stack_b.size - info->min_cost && - cont->stack_a.size + cont->stack_b.size >= 500) + if (info->b_info.pos > info->min_cost + && info->b_info.pos <= cont->stack_b.size - info->min_cost + && cont->stack_a.size + cont->stack_b.size >= 500) info->b_info.pos = cont->stack_b.size - info->min_cost; info->b_info.pos++; } - insert_elem_b(cont, moves_buff, info); - free(info); insert_b(cont, moves_buff); return ; @@ -47,23 +52,25 @@ static void insert_same_sign( do_rr(cont, moves_buff); } } - while (info->min_cost-- > 0) + else { - if (++info->a_info.dist0_best > 0) - do_rrb(cont, moves_buff); - else if (++info->b_info.dist0_best > 0) - do_rra(cont, moves_buff); - else - do_rrr(cont, moves_buff); + while (info->min_cost-- > 0) + { + if (++info->a_info.dist0_best > 0) + do_rrb(cont, moves_buff); + else if (++info->b_info.dist0_best > 0) + do_rra(cont, moves_buff); + else + do_rrr(cont, moves_buff); + } } - do_pa(cont, moves_buff); } static void insert_same_direction( t_main_cont *cont, t_deque *moves_buff, t_insert_info *info) { - if (info->min_cost == info->a_info.pos_best || - info->min_cost == info->b_info.pos_best) + if (info->min_cost == info->a_info.pos_best + || info->min_cost == info->b_info.pos_best) { while (info->min_cost-- > 0) { @@ -75,16 +82,18 @@ static void insert_same_direction( do_rr(cont, moves_buff); } } - while (info->min_cost-- > 0) + else { - if (++info->a_info.revpos_best > 0) - do_rrb(cont, moves_buff); - else if (++info->b_info.revpos_best > 0) - do_rra(cont, moves_buff); - else - do_rrr(cont, moves_buff); + while (info->min_cost-- > 0) + { + if (++info->a_info.revpos_best > 0) + do_rrb(cont, moves_buff); + else if (++info->b_info.revpos_best > 0) + do_rra(cont, moves_buff); + else + do_rrr(cont, moves_buff); + } } - do_pa(cont, moves_buff); } static void insert_indep_directions( @@ -102,8 +111,6 @@ static void insert_indep_directions( else while (info->b_info.dist0_best++ < 0) do_rrb(cont, moves_buff); - do_pa(cont, moves_buff); - } void insert_elem_b( @@ -122,48 +129,6 @@ void insert_elem_b( insert_same_direction(cont, moves_buff, info); else insert_indep_directions(cont, moves_buff, info); - return ; -} - -int calc_delta_two_values(t_main_cont *cont, int val_a, int val_b) -{ - int delta_insert; - int max_a; - int min_a; - int max_b; - int min_b; - - max_a = cont->stack_a.max_elem; - min_a = cont->stack_a.min_elem; - max_b = cont->stack_b.max_elem; - min_b = cont->stack_b.min_elem; - delta_insert = ft_abs(val_a - val_b); // is now equal to the diff between the two - //if bigger than half the absolute diff - if (delta_insert <= (ft_max(max_a, max_b) + 1) / 2) - return (delta_insert); - delta_insert = (max_a - val_a) + (val_b - min_b) + 1; - if (val_b > val_a) - delta_insert = (max_b - val_b) + (val_a - min_a) + 1; - return (delta_insert); -} - -//Need to add absolute distance from previous one -int calc_delta_insert(t_main_cont *cont, t_insert_info *info) -{ - t_iterator *iter; - int delta_insert; - - iter = malloc(sizeof(t_iterator)); - if (!iter) - exit_on_err("calc_delta_insert: iter error\n"); - set_iterator(iter, info->a_info.pos, cont->stack_a.size, 1); - iterate_once(iter, 1); - delta_insert = calc_delta_two_values( - cont, - cont->stack_a.elems[iter->index], - cont->stack_b.elems[iter->index]); - delta_insert = ft_min(delta_insert, \ - calc_delta_two_values(cont, info->a_info.val, info->b_info.val)); - free(iter); - return (delta_insert); + do_pa(cont, moves_buff); + return ; } diff --git a/src/insert_blocks.c b/src/insert_blocks.c index 19a7f07..a161583 100644 --- a/src/insert_blocks.c +++ b/src/insert_blocks.c @@ -1,65 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* insert_blocks.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- a_info.dist0_best > 0 || info->b_info.dist0_best > 0) + t_block_info info; + int i; + + info.curr_block_id = curr_block_id_a; + info.max_val = 0; + info.min_val = cont->stack_a.max_elem; + assign_block_median(&cont->stack_a, block_ids_a, &info); + i = 0; + while (i < block_ids_b->size) { - while (info->min_cost-- > 0) - { - if (--info->b_info.dist0_best < 0) - do_ra(cont, &cont->curr_moves); - else - { - if (--info->a_info.dist0_best < 0) - do_rb(cont, &cont->curr_moves); - else - do_rr(cont, &cont->curr_moves); - block_ids_b->add_last(block_ids_b, block_ids_b->elems[0]); - block_ids_b->remove_front(block_ids_b); - } - } + block_ids_b->elems[i] += 2; + i++; } - while (info->min_cost-- > 0) + while (block_id_is_in_stack(block_ids_a, curr_block_id_a)) { - if (++info->b_info.dist0_best > 0) - do_rra(cont, &cont->curr_moves); + if (block_ids_a->elems[0] == curr_block_id_a) + insert_curr_block_id(cont, block_ids_a, block_ids_b, &info); else - { - if (++info->a_info.dist0_best > 0) - do_rrb(cont, &cont->curr_moves); - else - do_rrr(cont, &cont->curr_moves); - block_ids_b->add_front(block_ids_b, block_ids_b->elems[block_ids_b->size - 1]); - block_ids_b->remove_last(block_ids_b); - } + rotate_to_block_id(cont, block_ids_a, block_ids_b, &info); } - do_pa(cont, &cont->curr_moves); - block_ids_b->remove_front(block_ids_b); + check_if_top_stack_b_is_smaller_than_median(cont, block_ids_b, &info); + return ; } - -void insert_same_direction( +void insert_block_rot( t_main_cont *cont, t_deque *block_ids_b, t_insert_info *info) { - if (info->min_cost == info->a_info.pos_best || - info->min_cost == info->b_info.pos_best) + while (info->min_cost-- > 0) { - while (info->min_cost-- > 0) + if (--info->b_info.pos_best < 0) + do_ra(cont, &cont->curr_moves); + else { - if (--info->b_info.pos_best < 0) - do_ra(cont, &cont->curr_moves); + if (--info->a_info.pos_best < 0) + do_rb(cont, &cont->curr_moves); else - { - if (--info->a_info.pos_best < 0) - do_rb(cont, &cont->curr_moves); - else - do_rr(cont, &cont->curr_moves); - block_ids_b->add_last(block_ids_b, block_ids_b->elems[0]); - block_ids_b->remove_front(block_ids_b); - } + do_rr(cont, &cont->curr_moves); + block_ids_b->add_last(block_ids_b, block_ids_b->elems[0]); + block_ids_b->remove_front(block_ids_b); } } + return ; +} + +void insert_block_rev_rot( + t_main_cont *cont, t_deque *block_ids_b, t_insert_info *info) +{ while (info->min_cost-- > 0) { if (++info->b_info.revpos_best > 0) @@ -70,15 +75,14 @@ void insert_same_direction( do_rrb(cont, &cont->curr_moves); else do_rrr(cont, &cont->curr_moves); - block_ids_b->add_front(block_ids_b, block_ids_b->elems[block_ids_b->size - 1]); + block_ids_b->add_front(block_ids_b, + block_ids_b->elems[block_ids_b->size - 1]); block_ids_b->remove_last(block_ids_b); } } - do_pa(cont, &cont->curr_moves); - block_ids_b->remove_front(block_ids_b); + return ; } - void insert_indep_directions( t_main_cont *cont, t_deque *block_ids_b, t_insert_info *info) { @@ -102,237 +106,36 @@ void insert_indep_directions( while (info->b_info.dist0_best++ < 0) { do_rrb(cont, &cont->curr_moves); - block_ids_b->add_front(block_ids_b, block_ids_b->elems[block_ids_b->size - 1]); + block_ids_b->add_front(block_ids_b, + block_ids_b->elems[block_ids_b->size - 1]); block_ids_b->remove_last(block_ids_b); } } - do_pa(cont, &cont->curr_moves); - block_ids_b->remove_front(block_ids_b); } void insert_block_elem_b( t_main_cont *cont, t_deque *block_ids_b, t_insert_info *info) { - t_stack_insert_info *a_info; - t_stack_insert_info *b_info; - if (cont->stack_b.size <= 0) return ; - a_info = &info->a_info; - b_info = &info->b_info; - if (cont->stack_b.size > 0) - { - //TO REMOVE - // if (info.min_cost < 0) - if (DEBUG && (info->min_cost < 0 || \ - (cont->stack_a.elems[0] > cont->stack_a.elems[1] && \ - cont->stack_a.elems[1] != cont->stack_a.min_elem))) - { - printf("wtf happened after\n"); - print_stacks(cont); - printf(YELLOW"info.a_info.val_best = %d \n"RESET_COL, info->a_info.val_best); - printf(YELLOW"info.a_info.pos_best = %d \n"RESET_COL, info->a_info.pos_best); - printf(YELLOW"info.a_info.revpos_best = %d \n"RESET_COL, info->a_info.revpos_best); - printf(YELLOW"info.b_info.val_best = %d \n"RESET_COL, info->b_info.val_best); - printf(YELLOW"info.b_info.pos_best = %d \n"RESET_COL, info->b_info.pos_best); - printf(YELLOW"info.b_info.revpos_best = %d \n"RESET_COL, info->b_info.revpos_best); - printf(YELLOW"info.a_info.dist0_best = %d \n"RESET_COL, info->a_info.dist0_best); - printf(YELLOW"info.b_info.dist0_best = %d \n"RESET_COL, info->b_info.dist0_best); - printf(YELLOW"info.min_cost = %d \n"RESET_COL, info->min_cost); - printf(YELLOW"info.min_delta_insert = %d \n"RESET_COL, info->min_delta_insert); - } - //EOREMOVE - } - // case for they both need to go in the same sense if (ft_same_sign(info->a_info.dist0_best, info->b_info.dist0_best)) - insert_same_sign(cont, block_ids_b, info); - // case where their respective best rotations are in opposite directions, - // but it's still better to move them in same direction - else if (get_cost_either_direction(info) < get_cost_indep_directions(info)) - insert_same_direction(cont, block_ids_b, info); - // case where they each do their own thing. that's okay too. - else - insert_indep_directions(cont, block_ids_b, info); - return ; -} - - -void insert_block_of_b_in_a(t_main_cont *cont, t_deque *block_ids_b, int block_id) -{ - t_insert_info *info; - - // print_stacks(cont); - // print_single_stack(block_ids_b); - - - if (!block_id_is_in_stack(block_ids_b, block_id)) - return ; - - info = malloc(sizeof(t_insert_info)); - if (!info) - exit_on_err("insert_b: insert_info error\n"); - - info->min_cost = INT_MAX; - info->min_delta_insert = INT_MAX; - info->b_info.pos = 0; - - while (info->b_info.pos < block_ids_b->size) - { - if (block_ids_b->elems[info->b_info.pos] == block_id) - update_insert_info(cont, info); - info->b_info.pos++; - } - - - - - // if (DEBUG) - // { - // printf(YELLOW"insert_b found best elem to be stack_b[%d] == %d\n"RESET_COL, info->b_info.pos_best, cont->stack_b.elems[info->b_info.pos_best]); - // printf(YELLOW"corresponding insert value was stack_a[%d] == %d\n"RESET_COL, info->a_info.pos_best, cont->stack_a.elems[info->a_info.pos_best]); - // printf(YELLOW"info.a_info.val_best = %d \n"RESET_COL, info->a_info.val_best); - // printf(YELLOW"info.a_info.pos_best = %d \n"RESET_COL, info->a_info.pos_best); - // printf(YELLOW"info.a_info.revpos_best = %d \n"RESET_COL, info->a_info.revpos_best); - // printf(YELLOW"info.b_info.val_best = %d \n"RESET_COL, info->b_info.val_best); - // printf(YELLOW"info.b_info.pos_best = %d \n"RESET_COL, info->b_info.pos_best); - // printf(YELLOW"info.b_info.revpos_best = %d \n"RESET_COL, info->b_info.revpos_best); - // printf(YELLOW"info.a_info.dist0_best = %d \n"RESET_COL, info->a_info.dist0_best); - // printf(YELLOW"info.b_info.dist0_best = %d \n"RESET_COL, info->b_info.dist0_best); - // printf(YELLOW"info.min_cost = %d \n"RESET_COL, info->min_cost); - // printf(YELLOW"info.min_delta_insert = %d \n"RESET_COL, info->min_delta_insert); - // } - - - if (cont->stack_b.size > 0) - { - //Needs to update block_ids_b... - insert_block_elem_b(cont, block_ids_b, info); - - // if (DEBUG) - // { - // printf(BLUE"last 5 moves: \n"RESET_COL); - // for (int i = 5; i && moves_b`uff->size > 5 - i; i--) - // print_move(cont->curr_moves.elems[cont->curr_moves.size - i]); - // } - // print_stacks(cont); - // print_single_stack(block_ids_b); - - if (DEBUG && !is_sorted(&cont->stack_a, cont->head_a.index)) - { - printf("fucked up right here\n"); - printf(BLUE"last 35 moves: \n"RESET_COL); - for (int i = 35; i && cont->curr_moves.size > 35 - i; i--) - print_move(cont->curr_moves.elems[cont->curr_moves.size - i]); - exit(1); - } - insert_block_of_b_in_a(cont, block_ids_b, block_id); - } - - //NEW - free(info); - info = NULL; - return ; -} - -void insert_curr_block_id( - t_main_cont *cont, - t_deque *block_ids_a, - t_deque *block_ids_b, - t_block_info *info) -{ - // if the first elem of stack_a is a candidate for push - while (cont->stack_a.elems[0] <= info->median_val && - has_smaller_than_median(&cont->stack_b, info->median_val, info->min_val) && - cont->stack_b.elems[0] > info->median_val && - cont->stack_b.elems[0] <= info->max_val) - { - do_rb(cont, &cont->curr_moves); - block_ids_b->add_last(block_ids_b, block_ids_b->elems[0]); - block_ids_b->remove_front(block_ids_b); - } - do_pb(cont, &cont->curr_moves); - if (cont->stack_b.elems[0] >= info->median_val) - block_ids_b->add_front(block_ids_b, 1); - else - block_ids_b->add_front(block_ids_b, 0); - block_ids_a->remove_front(block_ids_a); -} - -void rotate_to_block_id( - t_main_cont *cont, - t_deque *block_ids_a, - t_deque *block_ids_b, - t_block_info *info) -{ - int pos_a; - - pos_a = 0; - while (block_ids_a->elems[pos_a] != info->curr_block_id) - pos_a++; - if (has_smaller_than_median(&cont->stack_b, info->median_val, info->min_val)) - { - while (pos_a && cont->stack_b.elems[0] > info->median_val && cont->stack_b.elems[0] <= info->max_val) - { - do_rr(cont, &cont->curr_moves); - - block_ids_b->add_last(block_ids_b, block_ids_b->elems[0]); - block_ids_b->remove_front(block_ids_b); - - block_ids_a->add_last(block_ids_a, block_ids_a->elems[0]); - block_ids_a->remove_front(block_ids_a); - pos_a--; - } - } - while (pos_a) - { - do_ra(cont, &cont->curr_moves); - - block_ids_a->add_last(block_ids_a, block_ids_a->elems[0]); - block_ids_a->remove_front(block_ids_a); - pos_a--; - } - return ; -} - -void check_if_top_stack_b_is_smaller_than_median( - t_main_cont *cont, t_deque *block_ids, t_block_info *info) -{ - while (cont->stack_b.elems[0] > info->median_val - && cont->stack_b.elems[0] <= info->max_val) { - do_rb(cont, &cont->curr_moves); - block_ids->add_last(block_ids, block_ids->elems[0]); - block_ids->remove_front(block_ids); - } - return ; -} - -void insert_block_set_ids( - t_main_cont *cont, - t_deque *block_ids_a, - t_deque *block_ids_b, - int curr_block_id_a) -{ - t_block_info info; - int i; - - info.curr_block_id = curr_block_id_a; - info.max_val = 0; - info.min_val = cont->stack_a.max_elem; - info.median_val = calc_block_median(&cont->stack_a, block_ids_a, curr_block_id_a, &info.max_val, &info.min_val); - i = 0; - while (i < block_ids_b->size) - { - block_ids_b->elems[i] += 2; - i++; + if (info->a_info.dist0_best > 0 || info->b_info.dist0_best > 0) + insert_block_rot(cont, block_ids_b, info); + else + insert_block_rev_rot(cont, block_ids_b, info); } - while (block_id_is_in_stack(block_ids_a, curr_block_id_a)) + else if (get_cost_either_direction(info) < get_cost_indep_directions(info)) { - if (block_ids_a->elems[0] == curr_block_id_a) - insert_curr_block_id(cont, block_ids_a, block_ids_b, &info); + if (info->min_cost == info->a_info.pos_best + || info->min_cost == info->b_info.pos_best) + insert_block_rot(cont, block_ids_b, info); else - rotate_to_block_id(cont, block_ids_a, block_ids_b, &info); + insert_block_rev_rot(cont, block_ids_b, info); } - check_if_top_stack_b_is_smaller_than_median(cont, block_ids_b, &info); + else + insert_indep_directions(cont, block_ids_b, info); + do_pa(cont, &cont->curr_moves); + block_ids_b->remove_front(block_ids_b); return ; } diff --git a/src/insert_blocks_utils.c b/src/insert_blocks_utils.c new file mode 100644 index 0000000..6c5753d --- /dev/null +++ b/src/insert_blocks_utils.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* insert_blocks_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- elems[pos_a] != info->curr_block_id) + pos_a++; + if (has_smaller_than_median(&cont->stack_b, info)) + { + while (pos_a && cont->stack_b.elems[0] > info->median_val + && cont->stack_b.elems[0] <= info->max_val) + { + do_rr(cont, &cont->curr_moves); + block_ids_b->add_last(block_ids_b, block_ids_b->elems[0]); + block_ids_b->remove_front(block_ids_b); + block_ids_a->add_last(block_ids_a, block_ids_a->elems[0]); + block_ids_a->remove_front(block_ids_a); + pos_a--; + } + } + while (pos_a) + { + do_ra(cont, &cont->curr_moves); + block_ids_a->add_last(block_ids_a, block_ids_a->elems[0]); + block_ids_a->remove_front(block_ids_a); + pos_a--; + } +} + +void check_if_top_stack_b_is_smaller_than_median( + t_main_cont *cont, t_deque *block_ids, t_block_info *info) +{ + while (cont->stack_b.elems[0] > info->median_val + && cont->stack_b.elems[0] <= info->max_val) + { + do_rb(cont, &cont->curr_moves); + block_ids->add_last(block_ids, block_ids->elems[0]); + block_ids->remove_front(block_ids); + } + return ; +} + +void insert_block_of_b_in_a( + t_main_cont *cont, t_deque *block_ids_b, int block_id) +{ + t_insert_info *info; + + if (!block_id_is_in_stack(block_ids_b, block_id)) + return ; + info = ft_xalloc(sizeof(t_insert_info)); + info->min_cost = INT_MAX; + info->min_delta_insert = INT_MAX; + info->b_info.pos = 0; + while (info->b_info.pos < block_ids_b->size) + { + if (block_ids_b->elems[info->b_info.pos] == block_id) + update_insert_info(cont, info); + info->b_info.pos++; + } + if (cont->stack_b.size > 0) + { + insert_block_elem_b(cont, block_ids_b, info); + insert_block_of_b_in_a(cont, block_ids_b, block_id); + } + free(info); + info = NULL; + return ; +} + +void insert_curr_block_id( + t_main_cont *cont, + t_deque *block_ids_a, + t_deque *block_ids_b, + t_block_info *info) +{ + while (cont->stack_a.elems[0] <= info->median_val + && has_smaller_than_median(&cont->stack_b, info) + && cont->stack_b.elems[0] > info->median_val + && cont->stack_b.elems[0] <= info->max_val) + { + do_rb(cont, &cont->curr_moves); + block_ids_b->add_last(block_ids_b, block_ids_b->elems[0]); + block_ids_b->remove_front(block_ids_b); + } + do_pb(cont, &cont->curr_moves); + if (cont->stack_b.elems[0] >= info->median_val) + block_ids_b->add_front(block_ids_b, 1); + else + block_ids_b->add_front(block_ids_b, 0); + block_ids_a->remove_front(block_ids_a); +} diff --git a/src/insert_utils.c b/src/insert_utils.c index e6a2afd..483bc77 100644 --- a/src/insert_utils.c +++ b/src/insert_utils.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* insert_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- stack_a.max_elem; + assign_block_median(&cont->stack_a, block_ids, &info); + while (block_id_is_in_stack(block_ids, curr_block_id)) + { + if (block_ids->elems[0] == curr_block_id) + { + while (has_smaller_than_median(&cont->stack_b, &info) + && cont->stack_a.elems[0] <= info.median_val + && cont->stack_b.elems[0] > info.median_val + && cont->stack_b.elems[0] <= info.max_val) + do_rb(cont, &cont->curr_moves); + do_pb(cont, &cont->curr_moves); + block_ids->remove_front(block_ids); + } + else + rotate_block_split(cont, block_ids, &info); + } + while (cont->stack_b.elems[0] > info.median_val + && cont->stack_b.elems[0] <= info.max_val) + do_rb(cont, &cont->curr_moves); + return ; +} + +void insert_last_block(t_main_cont *cont, t_deque *moves_list) +{ + t_block_info info; + + info.max_val = cont->stack_a.max_elem; + info.min_val = cont->stack_a.min_elem; + info.median_val = (info.max_val - info.min_val) / 2; + while (cont->stack_a.size > 3) + { + do_pb(cont, &cont->curr_moves); + if (has_smaller_than_median(&cont->stack_b, &info) + && cont->stack_b.elems[0] > info.median_val + && cont->stack_b.elems[0] <= info.max_val) + do_rb(cont, moves_list); + } + if (!is_sorted(&cont->stack_a, cont->head_a.index)) + do_sa(cont, moves_list); + return ; +} diff --git a/src/iterators.c b/src/iterators.c index 800dbac..e33686e 100644 --- a/src/iterators.c +++ b/src/iterators.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* iterators.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- head = head; iterator->max_size = size; iterator->is_reverse = is_reverse; - //Will be incremented in the first loop to set at 0 once iterate starts iterator->nb_loops = -1; iterator->prev_index = -1; return ; @@ -24,7 +35,6 @@ void iterate(t_iterator *iterator) int incrementer; iterator->prev_index = iterator->index; - //-1 if direction == 1, +1 if direction == 0 incrementer = iterator->is_reverse * -1 + !iterator->is_reverse * 1; iterator->index += incrementer; if (incrementer == 1 && iterator->index > iterator->max_size - 1) @@ -45,12 +55,10 @@ void iterate_once(t_iterator *iterator, bool is_reverse) bool iterate_n_loops(t_iterator *iterator, int n) { int incrementer; - // static bool did_one_loop; if (iterator->index == iterator->head) iterator->nb_loops += 1; iterator->prev_index = iterator->index; - //-1 if direction == 1, +1 if direction == 0 incrementer = iterator->is_reverse * -1 + !iterator->is_reverse * 1; iterator->index += incrementer; if (incrementer == 1 && iterator->index > iterator->max_size - 1) diff --git a/src/lis_utils.c b/src/lis_utils.c new file mode 100644 index 0000000..9f77a3c --- /dev/null +++ b/src/lis_utils.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lis_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- size) + { + if (stack->elems[i] == val) + return (true); + i++; + } + return (false); +} + +void free_deque_list(t_deque **list, int size) +{ + int i; + + i = 0; + while (i < size) + { + list[i]->free_list(list[i]); + free (list[i]); + i++; + } + free (list); + return ; +} + +void rotate_stack_to_0(t_deque *stack, int pos) +{ + if (pos < stack->size - pos) + { + while (pos-- > 0) + { + rotate(stack); + } + } + else + { + while (pos++ < stack->size) + { + rev_rotate(stack); + } + } + return ; +} diff --git a/src/cherrypick.c b/src/longest_increas_subseq.c similarity index 61% rename from src/cherrypick.c rename to src/longest_increas_subseq.c index 6794dd2..f7d5e2a 100644 --- a/src/cherrypick.c +++ b/src/longest_increas_subseq.c @@ -1,23 +1,16 @@ -#include "push_swap.h" +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cherrypick.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- size - pos) - { - while (pos-- > 0) - { - rotate(stack); - } - } - else - { - while (pos++ < stack->size) - { - rev_rotate(stack); - } - } - return ; -} +#include "push_swap.h" int longest_increasing_subsequence(t_deque **list, t_deque *stack) { @@ -53,8 +46,7 @@ t_deque *get_ordered_vals(t_deque *stack) t_deque *staying_vals; int list_size; - list = ft_safealloc(stack->max_elem * sizeof(t_deque *)); - ft_bzero(list, stack->max_elem); + list = ft_xalloc(stack->max_elem * sizeof(t_deque *)); new_deque(&list[0]); list[0]->add_last(list[0], stack->elems[0]); list_size = longest_increasing_subsequence(list, stack); @@ -63,7 +55,7 @@ t_deque *get_ordered_vals(t_deque *stack) return (staying_vals); } -void normalize_staying_vals(t_main_cont *cont) +static void normalize_staying_vals(t_main_cont *cont) { int i; @@ -71,15 +63,19 @@ void normalize_staying_vals(t_main_cont *cont) while (i < cont->staying_vals.size) { if (cont->staying_vals.elems[0] > cont->stack_a.max_elem) - cont->staying_vals.add_last(&cont->staying_vals, cont->staying_vals.elems[0] - cont->stack_a.size - 1); + cont->staying_vals.add_last( + &cont->staying_vals, + cont->staying_vals.elems[0] - cont->stack_a.size - 1); else - cont->staying_vals.add_last(&cont->staying_vals, cont->staying_vals.elems[0]); + cont->staying_vals.add_last( + &cont->staying_vals, + cont->staying_vals.elems[0]); cont->staying_vals.remove_front(&cont->staying_vals); i++; } } -void assign_leaving_vals(t_main_cont *cont) +static void assign_leaving_vals(t_main_cont *cont) { int i; @@ -89,7 +85,9 @@ void assign_leaving_vals(t_main_cont *cont) if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) cont->leaving_vals.add_last(&cont->leaving_vals, -1); else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); + cont->leaving_vals.add_last( + &cont->leaving_vals, + cont->stack_a.elems[i]); i++; } } @@ -100,7 +98,6 @@ void assign_longest_increasing_subsequence(t_main_cont *cont) t_deque *staying_stack; int i; - // check nb_sorted for every val. staying_stack = clone_deque(&cont->stack_a); i = 0; while (i < cont->stack_a.size) @@ -116,7 +113,6 @@ void assign_longest_increasing_subsequence(t_main_cont *cont) i++; } normalize_staying_vals(cont); - //Free staying_stack staying_stack->free_list(staying_stack); free(staying_stack); assign_leaving_vals(cont); diff --git a/src/parse.c b/src/parse.c index 654657d..9e9d90c 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- 0 && int_to_long > INT_MAX) + || (sign < 0 && int_to_long * sign < INT_MIN)) + return (false); + } + return (true); +} + +static char **split_args(int argc, char *argv[]) +{ + int i; + char **args_split; + + if (argc == 2) + return (ft_split(argv[1], ' ')); + else + args_split = ft_xalloc((argc) * sizeof(char *)); + i = 0; + while (i < argc - 1) + { + args_split[i] = ft_strdup(argv[i + 1]); + if (!args_split[i]) + exit_on_err("Error\n"); + i++; + } + args_split[i] = NULL; + return (args_split); + return (ft_split(argv[1], ' ')); +} + +void parse_bonus(t_checker *checker, int argc, char *argv[]) +{ + char **args_split; + int i; + + args_split = split_args(argc, argv); + i = 0; + while (args_split[i] && is_an_int(args_split[i]) == true) + i++; + if (args_split[i] != NULL) + { + free(checker); + ft_free_split(args_split); + exit_on_err("Error\n"); + } + init_bonus(checker, args_split); + if (has_duplicates(checker->stack_a.elems, checker->stack_a.size)) + { + free_checker(checker); + ft_free_split(args_split); + exit_on_err("Error\n"); + } + ft_free_split(args_split); + return ; +} diff --git a/src/partition_leaving_vals.c b/src/partition_leaving_vals.c index a9e5de3..587c083 100644 --- a/src/partition_leaving_vals.c +++ b/src/partition_leaving_vals.c @@ -1,12 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* partition_leaving_vals.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- size) + { + if (staying_vals->elems[i] == -1) + block_ids->add_last(block_ids, -1); + else if (staying_vals->elems[i] > cutoff) + block_ids->add_last(block_ids, 1); + else + block_ids->add_last(block_ids, 0); + i++; + } + return ; +} + t_deque *get_leaving_vals_trimmed(t_deque *leaving_vals) { t_deque *trimmed_vals; int *temp_array; int i; - temp_array = ft_safealloc(leaving_vals->size * sizeof(int)); + temp_array = ft_xalloc(leaving_vals->size * sizeof(int)); ft_memset(temp_array, -1, leaving_vals->size * 4); i = 0; while (i < leaving_vals->size) @@ -27,67 +59,69 @@ t_deque *get_leaving_vals_trimmed(t_deque *leaving_vals) return (trimmed_vals); } -void partition_leaving_vals_n_blocks(t_deque *leaving_vals, t_deque *block_ids, int nb_blocks) +void assign_first_partition( + t_deque *leaving_vals, + t_deque *block_ids, + t_deque *trimmed_vals, + t_partition_info *info) { - t_deque *trimmed_vals; - int block_len; - int curr_block_id; - int curr_block_min; - int offset; - int i; - - trimmed_vals = get_leaving_vals_trimmed(leaving_vals); - offset = trimmed_vals->size % nb_blocks; - deque_reinit_list(block_ids); - block_len = trimmed_vals->size / nb_blocks + ((offset-- > 0) * 1); - curr_block_id = 0; - curr_block_min = 0; + int i; + i = 0; while (i < leaving_vals->size) { if (leaving_vals->elems[i] >= trimmed_vals->elems[0] && \ - leaving_vals->elems[i] <= trimmed_vals->elems[block_len - 1]) - block_ids->add_last(block_ids, curr_block_id); + leaving_vals->elems[i] <= trimmed_vals->elems[info->block_len - 1]) + block_ids->add_last(block_ids, info->curr_block_id); else block_ids->add_last(block_ids, leaving_vals->elems[i]); i++; } - while (++curr_block_id < nb_blocks) - { - curr_block_min += block_len; - block_len = trimmed_vals->size / nb_blocks + ((offset-- > 0) * 1); - i = 0; - while (i < leaving_vals->size) - { - if (leaving_vals->elems[i] >= trimmed_vals->elems[curr_block_min] && \ - leaving_vals->elems[i] <= trimmed_vals->elems[curr_block_min + block_len - 1]) - block_ids->add_last(block_ids, curr_block_id); - else - block_ids->add_last(block_ids, block_ids->elems[0]); - block_ids->remove_front(block_ids); - i++; - } - } - trimmed_vals->free_list(trimmed_vals); - free (trimmed_vals); - return ; } -void partition_leaving_vals_cutoff(t_deque *staying_vals, t_deque *block_ids, int cutoff) +void assign_partitions( + t_deque *leaving_vals, + t_deque *block_ids, + t_deque *trimmed_vals, + t_partition_info *info) { - int i; + int i; - deque_reinit_list(block_ids); i = 0; - while (i < staying_vals->size) + info->curr_block_min += info->block_len; + info->block_len = trimmed_vals->size / info->nb_blocks; + info->block_len += ((info->offset-- > 0) * 1); + while (i < leaving_vals->size) { - if (staying_vals->elems[i] == -1) - block_ids->add_last(block_ids, -1); - else if (staying_vals->elems[i] > cutoff) - block_ids->add_last(block_ids, 1); - else - block_ids->add_last(block_ids, 0); + if (leaving_vals->elems[i] >= trimmed_vals->elems[info->curr_block_min] + && leaving_vals->elems[i] + <= trimmed_vals->elems[info->curr_block_min + info->block_len - 1]) + block_ids->add_last(block_ids, info->curr_block_id); + else + block_ids->add_last(block_ids, block_ids->elems[0]); + block_ids->remove_front(block_ids); i++; } +} + +void partition_leaving_vals_n_blocks( + t_deque *leaving_vals, t_deque *block_ids, int nb_blocks) +{ + t_deque *trimmed_vals; + t_partition_info info; + + trimmed_vals = get_leaving_vals_trimmed(leaving_vals); + info.offset = trimmed_vals->size % nb_blocks; + deque_reinit_list(block_ids); + info.block_len = trimmed_vals->size / nb_blocks; + info.block_len += ((info.offset-- > 0) * 1); + info.curr_block_id = 0; + info.curr_block_min = 0; + info.nb_blocks = nb_blocks; + assign_first_partition(leaving_vals, block_ids, trimmed_vals, &info); + while (++info.curr_block_id < nb_blocks) + assign_partitions(leaving_vals, block_ids, trimmed_vals, &info); + trimmed_vals->free_list(trimmed_vals); + free (trimmed_vals); return ; } diff --git a/src/partition_stack.c b/src/partition_stack.c index 5800887..cabe852 100644 --- a/src/partition_stack.c +++ b/src/partition_stack.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* partition_stack.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- size) + if ((move >> 16) & 0xFF) { - printf("|%-3d|", i); - if (i < stack->size) - printf("\033[38;5;5m %-3d \033[0;0;0m|\n", stack->elems[i]); - else - printf("\t\033[0m|\n"); - i++; + c = (char)((move >> 16) & 0xFF); + write(1, &c, 1); } - printf("+---+-----+\n"); + c = (char)((move >> 8) & 0xFF); + write(1, &c, 1); + c = (char)(move & 0xFF); + write(1, &c, 1); + write(1, "\n", 1); + return ; } -void print_stacks(t_main_cont *cont) +void print_all_moves(t_deque *curr_moves) { - int *stack_a; - int *stack_b; - int size_a; - int size_b; int i; - printf(" |\033[38;5;5m [A] \033[0m|\033[38;5;6m [B] \033[0;0;0m|\n"); - printf("+---+-----+-----+\n"); i = 0; - stack_a = cont->stack_a.elems; - stack_b = cont->stack_b.elems; - size_a = cont->stack_a.size; - size_b = cont->stack_b.size; - while (i < size_a || i < size_b) - { - printf("|%-3d|", i); - if (i < size_a) - printf("\033[38;5;5m %-3d \033[0;0;0m", stack_a[i]); - else - printf(" \033[0m"); - if (i < size_b) - printf("|\033[38;5;6m %-3d \033[0m|\n", stack_b[i]); - else - printf("| |\n\033[0m"); - i++; - } - printf("+---+-----+-----+\n"); -} - -void print_stacks_info(t_main_cont *cont) -{ - print_stacks(cont); - - printf(YELLOW"**stack_a**\n"); - printf("\tmax elem in a = %d\n", cont->stack_a.max_elem); - printf("\tmin elem in a = %d\n", cont->stack_a.min_elem); - printf("\tpos: %d\n", cont->head_a.index); - printf("\tsize of stack_a = %d\n", cont->stack_a.size); - - printf(YELLOW"**stack_b**\n"); - printf("\tmax elem in b = %d\n", cont->stack_b.max_elem); - printf("\tmin elem in b = %d\n", cont->stack_b.min_elem); - printf("\tpos: %d\n", cont->head_b.index); - printf("\tsize of stack_b = %d\n"RESET_COL, cont->stack_b.size); - - printf(CYAN"\t==> CURR TOTAL: %d \n"RESET_COL, cont->curr_moves.size); - print_all_moves(&cont->curr_moves); - printf(BLUE"\t==> BEST TOTAL: %d \n"RESET_COL, cont->best_moves.size); - print_all_moves(&cont->best_moves); - - return ; -} - -void print_move(int move) -{ - char c; - - if (move >> 16 & 0xFF) - { - c = (char)(move >> 16 & 0xFF); - write(1, &c, 1); - } - c = (char)(move >> 8 & 0xFF); - write(1, &c, 1); - c = (char)(move & 0xFF); - write(1, &c, 1); - write(1, "\n", 1); - return; -} - -void print_all_moves(t_deque *curr_moves) -{ - int pos_of_the_move_thing_in_the_list_thing; - - pos_of_the_move_thing_in_the_list_thing = 0; if (curr_moves->size == INT_MAX) return ; - while (pos_of_the_move_thing_in_the_list_thing < curr_moves->size) - { - print_move(curr_moves->elems[pos_of_the_move_thing_in_the_list_thing]); - pos_of_the_move_thing_in_the_list_thing++; - } - if (DEBUG) + while (i < curr_moves->size) { - if (!pos_of_the_move_thing_in_the_list_thing) - printf(RESET_COL"\t====== No move stored ======\n"); - else - printf(GREEN"\t====== Total nb_moves == %d ======\n"RESET_COL, pos_of_the_move_thing_in_the_list_thing); + print_move(curr_moves->elems[i]); + i++; } - return ; + return ; } diff --git a/src/push_swap.c b/src/push_swap.c index c130a0e..93a0cb4 100644 --- a/src/push_swap.c +++ b/src/push_swap.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- stack_a.size <= 7) - sort(cont); - - assign_longest_increasing_subsequence(cont); - - run_algorithms(cont); - + exit(EXIT_SUCCESS); + cont = ft_xalloc(sizeof(t_main_cont)); + parse(cont, argc, argv); + sort(cont); print_all_moves(&cont->best_moves); cleanup(cont); - return (EXIT_SUCCESS); } diff --git a/src/px.c b/src/px.c index 0e1753b..e43fcd2 100644 --- a/src/px.c +++ b/src/px.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* px.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- head_a, get_pos_smallest_val(&cont->stack_a), cont->stack_a.size, 0); - set_iterator(&cont->head_b, get_pos_smallest_val(&cont->stack_b), cont->stack_b.size, 1); + set_iterator( + &cont->head_a, + get_pos_smallest_val(&cont->stack_a), cont->stack_a.size, 0); + set_iterator( + &cont->head_b, + get_pos_smallest_val(&cont->stack_b), cont->stack_b.size, 1); return ; } -void do_pa(t_main_cont *cont, t_deque *curr_moves) +void do_pa(t_main_cont *cont, t_deque *curr_moves) { push(&cont->stack_b, &cont->stack_a); if (curr_moves) curr_moves->add_last(curr_moves, PA); - push_update_iterators(cont); - return ; } -void do_pb(t_main_cont *cont, t_deque *curr_moves) +void do_pb(t_main_cont *cont, t_deque *curr_moves) { push(&cont->stack_a, &cont->stack_b); if (curr_moves) curr_moves->add_last(curr_moves, PB); - push_update_iterators(cont); - return ; } diff --git a/src/px_bonus.c b/src/px_bonus.c new file mode 100644 index 0000000..a7e959a --- /dev/null +++ b/src/px_bonus.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* px_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- add_front(stack_dest, stack_src->elems[0]); + stack_src->remove_front(stack_src); + return ; +} + +void checker_pa(t_checker *checker) +{ + push(&checker->stack_b, &checker->stack_a); + return ; +} + +void checker_pb(t_checker *checker) +{ + push(&checker->stack_a, &checker->stack_b); + return ; +} diff --git a/src/rrx.c b/src/rrx.c index a5d91f8..9478d8e 100644 --- a/src/rrx.c +++ b/src/rrx.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rrx.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- stack_a); if (curr_moves) @@ -22,7 +34,7 @@ void do_rra(t_main_cont *cont, t_deque *curr_moves) return ; } -void do_rrb(t_main_cont *cont, t_deque *curr_moves) +void do_rrb(t_main_cont *cont, t_deque *curr_moves) { rev_rotate(&cont->stack_b); if (curr_moves) diff --git a/src/rrx_bonus.c b/src/rrx_bonus.c new file mode 100644 index 0000000..74c776b --- /dev/null +++ b/src/rrx_bonus.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rrx_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- add_front(stack, stack->elems[stack->size - 1]); + stack->remove_last(stack); + return ; +} + +void checker_rra(t_checker *checker) +{ + rev_rotate(&checker->stack_a); + return ; +} + +void checker_rrb(t_checker *checker) +{ + rev_rotate(&checker->stack_b); + return ; +} + +void checker_rrr(t_checker *checker) +{ + rev_rotate(&checker->stack_a); + rev_rotate(&checker->stack_b); + return ; +} diff --git a/src/rx.c b/src/rx.c index 5e07db1..4d86cb5 100644 --- a/src/rx.c +++ b/src/rx.c @@ -6,7 +6,7 @@ /* By: iyahoui- stack_a); if (curr_moves) curr_moves->add_last(curr_moves, RA); - rotate_update_iterators(&cont->head_a); return ; } -void do_rb(t_main_cont *cont, t_deque *curr_moves) +void do_rb(t_main_cont *cont, t_deque *curr_moves) { rotate(&cont->stack_b); if (curr_moves) @@ -50,7 +49,6 @@ void do_rr(t_main_cont *cont, t_deque *curr_moves) rotate(&cont->stack_b); if (curr_moves) curr_moves->add_last(curr_moves, RR); - rotate_update_iterators(&cont->head_a); rotate_update_iterators(&cont->head_b); return ; diff --git a/src/rx_bonus.c b/src/rx_bonus.c new file mode 100644 index 0000000..db3602c --- /dev/null +++ b/src/rx_bonus.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rx_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- add_last(stack, stack->elems[0]); + stack->remove_front(stack); + return ; +} + +void checker_ra(t_checker *checker) +{ + rotate(&checker->stack_a); + return ; +} + +void checker_rb(t_checker *checker) +{ + rotate(&checker->stack_b); + return ; +} + +void checker_rr(t_checker *checker) +{ + rotate(&checker->stack_a); + rotate(&checker->stack_b); + return ; +} diff --git a/src/sort.c b/src/sort.c index 64a146a..5dd3f2e 100644 --- a/src/sort.c +++ b/src/sort.c @@ -1,5 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- stack_a.size == 2 && !is_sorted(&cont->stack_a, 0)) @@ -15,12 +36,5 @@ void sort(t_main_cont *cont) sort_small(cont); else sort_big(cont); - print_all_moves(&cont->best_moves); - cleanup(cont); - exit(EXIT_SUCCESS); return ; } - -//Stopped here b/c Bernard is on his way -//I'm at: the else in try_sort_small at the end of the while is an error -//Then need to test behaviour for single stack 4 3 2 1 0 ==> should do invert_4 \ No newline at end of file diff --git a/src/sort_big.c b/src/sort_big.c deleted file mode 100644 index e688b4d..0000000 --- a/src/sort_big.c +++ /dev/null @@ -1,161 +0,0 @@ -#include "push_swap.h" - -bool block_id_is_in_stack(t_deque *block_ids, int id) -{ - int i; - - i = 0; - while (i < block_ids->size) - { - if(block_ids->elems[i] == id) - return (true); - i++; - } - return (false); -} - -int calc_block_median(t_deque *stack, t_deque *block_ids, int curr_block_id, int *max_val, int *min_val) -{ - int median_val; - int curr_pos; - - median_val = 0; - curr_pos = 0; - while (curr_pos < stack->size) - { - if (block_ids->elems[curr_pos] == curr_block_id) - { - if (stack->elems[curr_pos] < *min_val) - *min_val = stack->elems[curr_pos]; - else if (stack->elems[curr_pos] > *max_val) - *max_val = stack->elems[curr_pos]; - } - curr_pos++; - } - median_val = (*max_val - *min_val) / 2 + *min_val; - return (median_val); -} - -bool has_smaller_than_median(t_deque *stack, int median_val, int min_val) -{ - int curr_pos; - - curr_pos = 0; - while (curr_pos < stack->size) - { - if (stack->elems[curr_pos] >= min_val && stack->elems[curr_pos] <= median_val) - return (true); - curr_pos++; - } - return (false); -} - -void rotate_block_split(t_main_cont *cont, t_deque *block_ids, int curr_block_id, int max_val, int min_val, int median_val) -{ - int pos_a; - - pos_a = 0; - while (block_ids->elems[pos_a] != curr_block_id) - pos_a++; - if (has_smaller_than_median(&cont->stack_b, median_val, min_val)) - { - while (pos_a && cont->stack_b.elems[0] > median_val && cont->stack_b.elems[0] <= max_val) - { - do_rr(cont, &cont->curr_moves); - block_ids->add_last(block_ids, block_ids->elems[0]); - block_ids->remove_front(block_ids); - pos_a--; - } - } - while (pos_a) - { - do_ra(cont, &cont->curr_moves); - block_ids->add_last(block_ids, block_ids->elems[0]); - block_ids->remove_front(block_ids); - pos_a--; - } - return ; -} - -void insert_block_of_a_in_b(t_main_cont *cont, t_deque *block_ids, int curr_block_id) -{ - int median_val; - int max_val; - int min_val; - - max_val = 0; - min_val = cont->stack_a.max_elem; - median_val = calc_block_median(&cont->stack_a, block_ids, curr_block_id, &max_val, &min_val); - while (block_id_is_in_stack(block_ids, curr_block_id)) - { - if (block_ids->elems[0] == curr_block_id) - { - while (has_smaller_than_median(&cont->stack_b, median_val, min_val) && - cont->stack_a.elems[0] <= median_val && - cont->stack_b.elems[0] > median_val && cont->stack_b.elems[0] <= max_val) - do_rb(cont, &cont->curr_moves); - do_pb(cont, &cont->curr_moves); - block_ids->remove_front(block_ids); - } - else - rotate_block_split(cont, block_ids, curr_block_id, max_val, min_val, median_val); - } - while (cont->stack_b.elems[0] > median_val && cont->stack_b.elems[0] <= max_val) - do_rb(cont, &cont->curr_moves); - return ; -} - -void sort_big(t_main_cont *cont) -{ - // int elem_0; - int initial_size; - // int elem_1; - // int elem_last; - - initial_size = cont->stack_a.size; - - // if (stack->size == 100) - // blind_push(cont, &cont->curr_moves); - // else () - - // assign_divisions - - // insert_a needs to be ONLY called after segmentation - - - insert_b(cont, &cont->best_moves); - rotate_to_0_in_a(cont, &cont->best_moves, cont->head_a.index); -} - -void insert_last_block(t_main_cont *cont, t_deque *moves_list) -{ - int median_val; - int max_val; - int min_val; - - max_val = cont->stack_a.max_elem; - min_val = cont->stack_a.min_elem; - median_val = (max_val - min_val) / 2; - while (cont->stack_a.size > 3) - { - do_pb(cont, &cont->curr_moves); - if (has_smaller_than_median(&cont->stack_b, median_val, min_val) && cont->stack_b.elems[0] > median_val && cont->stack_b.elems[0] <= max_val) - do_rb(cont, moves_list); - } - if (!is_sorted(&cont->stack_a, cont->head_a.index)) - do_sa(cont, moves_list); - return ; - -} - - -void blind_push(t_main_cont *cont, t_deque *moves_list) -{ - while (!is_sorted(&cont->stack_a, cont->head_a.index) && cont->stack_a.size > 3) - { - do_pb(cont, moves_list); - } - if (!is_sorted(&cont->stack_a, cont->head_a.index)) - do_sa(cont, moves_list); - return ; -} \ No newline at end of file diff --git a/src/try_x.c b/src/sort_small.c similarity index 71% rename from src/try_x.c rename to src/sort_small.c index 7e84e48..6cc26d7 100644 --- a/src/try_x.c +++ b/src/sort_small.c @@ -1,9 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* try_x.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- stack_a, cont->head_a.index)) { return (false); @@ -95,7 +107,6 @@ bool try_invert_4(t_main_cont *cont) do_ra(cont, moves_buff); do_ra(cont, moves_buff); do_sa(cont, moves_buff); - if (try_solution(cont, moves_buff)) { return (true); diff --git a/src/sort_utils.c b/src/sort_utils.c index e8b73c2..cfcd353 100644 --- a/src/sort_utils.c +++ b/src/sort_utils.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- stack_a.size; - printf(YELLOW"rotate_to_pos0=======> pos = %d, revpos = %d\n", pos, revpos); - if (pos < cont->stack_a.size - pos) - printf("Choosing ra; %d < %d\n"RESET_COL, pos, -revpos); - else - printf("Choosing rra; %d >= %d\n"RESET_COL, pos, -revpos); - } if (pos < cont->stack_a.size - pos) while (pos-- > 0) do_ra(cont, curr_moves); diff --git a/src/split_in_blocks.c b/src/split_in_blocks.c index e71b33b..cb18dbd 100644 --- a/src/split_in_blocks.c +++ b/src/split_in_blocks.c @@ -1,7 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* split_in_blocks.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- curr_moves); rotate_to_0_in_a(cont, &cont->curr_moves, cont->head_a.index); if (cont->curr_moves.size < cont->best_moves.size) @@ -37,21 +48,15 @@ void split_in_blocks(t_main_cont *cont) new_deque(&block_ids_a); new_deque(&block_ids_b); - nb_blocks = 2; while (nb_blocks <= 6) { split_in_n_blocks(cont, block_ids_a, block_ids_b, nb_blocks); nb_blocks++; } - - // Free block_ids_a block_ids_a->free_list(block_ids_a); free(block_ids_a); - - // Free block_ids_b block_ids_b->free_list(block_ids_b); free(block_ids_b); - return ; } diff --git a/src/split_in_two.c b/src/split_in_two.c index 21fd074..e38f3af 100644 --- a/src/split_in_two.c +++ b/src/split_in_two.c @@ -1,23 +1,19 @@ -#include "push_swap.h" - -int max_elem_leaving(t_main_cont *cont, t_deque *leaving_vals) -{ - int i; - int max_elem; +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* split_in_two.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- size) - { - if (leaving_vals->elems[i] > max_elem - && !is_in_stack(&cont->stack_b, leaving_vals->elems[i])) - max_elem = leaving_vals->elems[i]; - i++; - } - return (max_elem); -} +#include "push_swap.h" -void try_single_cutoff(t_main_cont *cont, t_deque *block_ids, float cutoff) +static void try_single_cutoff( + t_main_cont *cont, t_deque *block_ids, float cutoff) { partition_leaving_vals_cutoff( &cont->leaving_vals, @@ -35,163 +31,16 @@ void try_single_cutoff(t_main_cont *cont, t_deque *block_ids, float cutoff) return ; } -void split_in_two(t_main_cont *cont) +static void try_multiple_cutoffs_body( + t_main_cont *cont, t_deque *block_ids, float cutoff, int id_to_push) { - t_deque *block_ids; - int i; - - new_deque(&block_ids); - - try_single_cutoff(cont, block_ids, 5 / 8); - try_single_cutoff(cont, block_ids, 13 / 24); - try_single_cutoff(cont, block_ids, 11 / 24); - try_single_cutoff(cont, block_ids, 3 / 8); - try_single_cutoff(cont, block_ids, 7 / 24); - - - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 3 / 5); - insert_block_of_a_in_b(cont, block_ids, 1); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 1 / 2); - insert_block_of_a_in_b(cont, block_ids, 1); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 2 / 5); - insert_block_of_a_in_b(cont, block_ids, 1); - insert_block_of_a_in_b(cont, block_ids, 0); - insert_b(cont, &cont->curr_moves); - rotate_to_0_in_a(cont, &cont->curr_moves, cont->head_a.index); - if (cont->curr_moves.size < cont->best_moves.size) - { - copy_deque(&cont->curr_moves, &cont->best_moves); - } - // undo_moves(cont, &cont->curr_moves); - copy_deque(&cont->initial_stack, &cont->stack_a); - deque_reinit_list(&cont->stack_b); - deque_reinit_list(&cont->curr_moves); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } - - - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 3 / 5); - insert_block_of_a_in_b(cont, block_ids, 1); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 2 / 5); - insert_block_of_a_in_b(cont, block_ids, 1); - insert_block_of_a_in_b(cont, block_ids, 0); - insert_b(cont, &cont->curr_moves); - rotate_to_0_in_a(cont, &cont->curr_moves, cont->head_a.index); - if (cont->curr_moves.size < cont->best_moves.size) - { - copy_deque(&cont->curr_moves, &cont->best_moves); - } - // undo_moves(cont, &cont->curr_moves); - copy_deque(&cont->initial_stack, &cont->stack_a); - deque_reinit_list(&cont->stack_b); - deque_reinit_list(&cont->curr_moves); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } - + int i; partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 3 / 5); - insert_block_of_a_in_b(cont, block_ids, 1); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 1 / 2); - insert_block_of_a_in_b(cont, block_ids, 1); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 1 / 2); - insert_block_of_a_in_b(cont, block_ids, 1); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 2 / 5); - insert_block_of_a_in_b(cont, block_ids, 1); - insert_block_of_a_in_b(cont, block_ids, 0); - insert_b(cont, &cont->curr_moves); - rotate_to_0_in_a(cont, &cont->curr_moves, cont->head_a.index); - if (cont->curr_moves.size < cont->best_moves.size) - { - copy_deque(&cont->curr_moves, &cont->best_moves); - } - copy_deque(&cont->initial_stack, &cont->stack_a); - deque_reinit_list(&cont->stack_b); - deque_reinit_list(&cont->curr_moves); + &cont->leaving_vals, + block_ids, + (int)(max_elem_leaving(cont, &cont->leaving_vals) * cutoff)); + insert_block_of_a_in_b(cont, block_ids, id_to_push); deque_reinit_list(&cont->leaving_vals); i = 0; while (i < cont->stack_a.size) @@ -199,50 +48,29 @@ void split_in_two(t_main_cont *cont) if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) cont->leaving_vals.add_last(&cont->leaving_vals, -1); else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); + cont->leaving_vals.add_last( + &cont->leaving_vals, + cont->stack_a.elems[i]); i++; } +} +static void try_multiple_cutoffs_footer( + t_main_cont *cont, t_deque *block_ids, float cutoff, int id_to_push) +{ + int i; partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 1 / 2); - insert_block_of_a_in_b(cont, block_ids, 0); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 1 / 2); - insert_block_of_a_in_b(cont, block_ids, 0); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 1 / 2); - insert_block_of_a_in_b(cont, block_ids, 0); - insert_block_of_a_in_b(cont, block_ids, 1); + &cont->leaving_vals, block_ids, + (int)(max_elem_leaving(cont, &cont->leaving_vals) * cutoff)); + insert_block_of_a_in_b(cont, block_ids, id_to_push); + insert_block_of_a_in_b(cont, block_ids, (id_to_push + 1) % 2); insert_b(cont, &cont->curr_moves); rotate_to_0_in_a(cont, &cont->curr_moves, cont->head_a.index); if (cont->curr_moves.size < cont->best_moves.size) - { copy_deque(&cont->curr_moves, &cont->best_moves); - } - // undo_moves(cont, &cont->curr_moves); - copy_deque(&cont->initial_stack, &cont->stack_a); - deque_reinit_list(&cont->stack_b); + copy_deque(&cont->initial_stack, &cont->stack_a); + deque_reinit_list(&cont->stack_b); deque_reinit_list(&cont->curr_moves); deque_reinit_list(&cont->leaving_vals); i = 0; @@ -251,65 +79,52 @@ void split_in_two(t_main_cont *cont) if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) cont->leaving_vals.add_last(&cont->leaving_vals, -1); else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); + cont->leaving_vals.add_last( + &cont->leaving_vals, cont->stack_a.elems[i]); i++; - } + } +} +void split_in_two_mult_cutoffs(t_main_cont *cont, t_deque *block_ids) +{ + try_multiple_cutoffs_body(cont, block_ids, 0.6F, 1); + try_multiple_cutoffs_body(cont, block_ids, 0.5F, 1); + try_multiple_cutoffs_body(cont, block_ids, 0.5F, 1); + try_multiple_cutoffs_footer(cont, block_ids, 0.4F, 1); + try_multiple_cutoffs_body(cont, block_ids, 0.6F, 1); + try_multiple_cutoffs_body(cont, block_ids, 0.5F, 1); + try_multiple_cutoffs_footer(cont, block_ids, 0.4F, 1); + try_multiple_cutoffs_body(cont, block_ids, 0.6F, 1); + try_multiple_cutoffs_footer(cont, block_ids, 0.4F, 1); + try_multiple_cutoffs_body(cont, block_ids, 0.6F, 1); + try_multiple_cutoffs_body(cont, block_ids, 0.5F, 1); + try_multiple_cutoffs_body(cont, block_ids, 0.5F, 1); + try_multiple_cutoffs_footer(cont, block_ids, 0.4F, 0); + try_multiple_cutoffs_body(cont, block_ids, 0.6F, 1); + try_multiple_cutoffs_body(cont, block_ids, 0.5F, 1); + try_multiple_cutoffs_footer(cont, block_ids, 0.4F, 0); + try_multiple_cutoffs_body(cont, block_ids, 0.6F, 1); + try_multiple_cutoffs_footer(cont, block_ids, 0.4F, 0); + try_multiple_cutoffs_body(cont, block_ids, 0.5F, 1); + try_multiple_cutoffs_body(cont, block_ids, 0.5F, 1); + try_multiple_cutoffs_footer(cont, block_ids, 0.5F, 1); + try_multiple_cutoffs_body(cont, block_ids, 0.5F, 0); + try_multiple_cutoffs_body(cont, block_ids, 0.5F, 0); + try_multiple_cutoffs_footer(cont, block_ids, 0.5F, 0); +} - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 1 / 2); - insert_block_of_a_in_b(cont, block_ids, 1); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 1 / 2); - insert_block_of_a_in_b(cont, block_ids, 1); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } - partition_leaving_vals_cutoff( - &cont->leaving_vals, block_ids, max_elem_leaving(cont, &cont->leaving_vals) * 1 / 2); - insert_block_of_a_in_b(cont, block_ids, 1); - insert_block_of_a_in_b(cont, block_ids, 0); - insert_b(cont, &cont->curr_moves); - rotate_to_0_in_a(cont, &cont->curr_moves, cont->head_a.index); - if (cont->curr_moves.size < cont->best_moves.size) - { - copy_deque(&cont->curr_moves, &cont->best_moves); - } - // undo_moves(cont, &cont->curr_moves); - copy_deque(&cont->initial_stack, &cont->stack_a); - deque_reinit_list(&cont->stack_b); - deque_reinit_list(&cont->curr_moves); - deque_reinit_list(&cont->leaving_vals); - i = 0; - while (i < cont->stack_a.size) - { - if (is_in_stack(&cont->staying_vals, cont->stack_a.elems[i])) - cont->leaving_vals.add_last(&cont->leaving_vals, -1); - else - cont->leaving_vals.add_last(&cont->leaving_vals, cont->stack_a.elems[i]); - i++; - } +void split_in_two(t_main_cont *cont) +{ + t_deque *block_ids; - // Free block_ids + new_deque(&block_ids); + try_single_cutoff(cont, block_ids, 5 / 8); + try_single_cutoff(cont, block_ids, 13 / 24); + try_single_cutoff(cont, block_ids, 11 / 24); + try_single_cutoff(cont, block_ids, 3 / 8); + try_single_cutoff(cont, block_ids, 7 / 24); + split_in_two_mult_cutoffs(cont, block_ids); block_ids->free_list(block_ids); free(block_ids); - return ; -} \ No newline at end of file +} diff --git a/src/split_in_two_utils.c b/src/split_in_two_utils.c new file mode 100644 index 0000000..c793b52 --- /dev/null +++ b/src/split_in_two_utils.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* split_in_two_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- size) + { + if (leaving_vals->elems[i] > max_elem + && !is_in_stack(&cont->stack_b, leaving_vals->elems[i])) + max_elem = leaving_vals->elems[i]; + i++; + } + return (max_elem); +} diff --git a/src/sx.c b/src/sx.c index 2ff7cf5..0eab0a9 100644 --- a/src/sx.c +++ b/src/sx.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sx.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- stack_a); if (curr_moves) curr_moves->add_last(curr_moves, SA); - swap_update_iterator(&cont->head_a); - return ; } -void do_sb(t_main_cont *cont, t_deque *curr_moves) +void do_sb(t_main_cont *cont, t_deque *curr_moves) { swap(&cont->stack_b); if (curr_moves) curr_moves->add_last(curr_moves, SB); - swap_update_iterator(&cont->head_b); - return ; } -void do_ss(t_main_cont *cont, t_deque *curr_moves) +void do_ss(t_main_cont *cont, t_deque *curr_moves) { swap(&cont->stack_a); swap(&cont->stack_b); if (curr_moves) curr_moves->add_last(curr_moves, SS); - swap_update_iterator(&cont->head_a); swap_update_iterator(&cont->head_b); - return ; -} \ No newline at end of file +} diff --git a/src/sx_bonus.c b/src/sx_bonus.c new file mode 100644 index 0000000..634c1dc --- /dev/null +++ b/src/sx_bonus.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sx_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- elems[0]; + stack->set_elem(stack, 0, stack->elems[1]); + stack->set_elem(stack, 1, temp); + return ; +} + +void checker_sa(t_checker *checker) +{ + swap(&checker->stack_a); + return ; +} + +void checker_sb(t_checker *checker) +{ + swap(&checker->stack_b); + return ; +} + +void checker_ss(t_checker *checker) +{ + swap(&checker->stack_a); + swap(&checker->stack_b); + return ; +} diff --git a/src/undo_moves.c b/src/undo_moves.c index 13cc7bd..5e1ae48 100644 --- a/src/undo_moves.c +++ b/src/undo_moves.c @@ -1,36 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* undo_moves.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iyahoui- 0) + 3 * (s => 0) == 0 + 3*0 == 0 | rev_fcts[0] ==> &do_sa - * REV_SB = 1 | (b => 1) + 3 * (s => 0) == 1 + 3*0 == 1 | rev_fcts[1] ==> &do_sb - * REV_SS = 2 | (r/s => 2) + 3 * (s => 0) == 2 + 3*0 == 2 | rev_fcts[5] ==> &do_ss - * REV_RA = 3 | {...} - * REV_RB = 4 | - * REV_RR = 5 | (r/s => 2) + 3 * (r => 1) == 2 + 3*1 == 5 | rev_fcts[5] ==> &do_rrr - * REV_RRA = 6 | {...} - * REV_RRB = 7 | (b => 1) + 3 * (rr => q => 2) == 1 + 3*2 == 7| rev_fcts[7] ==> &do_rb - * REV_RRR = 8 | {...} - * REV_PA = 9 | - * REV_PB = 10 | (b => 1) + 3 * (p => 3) == 1 + 3*3 == 10 | rev_fcts[10] ==> &do_pa + * SA = 0 | (a=>0) + 3 * (s=>0) = 0 + 3*0 = 0 | rev_fcts[0] => &do_sa + * SB = 1 | (b=>1) + 3 * (s=>0) = 1 + 3*0 = 1 | rev_fcts[1] => &do_sb + * SS = 2 | (r/s=>2) + 3 * (s=>0) = 2 + 3*0 = 2 | rev_fcts[5] => &do_ss + * RA = 3 | {...} + * RB = 4 | + * RR = 5 | (r/s=>2) + 3 * (r=>1) = 2+ 3*1 = 5 | rev_fcts[5] => &do_rrr + * RRA = 6 | {...} + * RRB = 7 | (b=>1) + 3 * (rr=>q=>2) = 1 + 3*2 = 7| rev_fcts[7] => &do_rb + * RRR = 8 | {...} + * PA = 9 | + * PB = 10 | (b=>1) + 3 * (p=>3) = 1 + 3*3 = 10 | rev_fcts[10] ==> &do_pa */ - -//Could just be: -// return ((((move & 0xFF)) / 10 - 88) + (-3 * ((move >> 8) & 0xFF) - 73)); int convert_move_to_index(int move) { int move_to_index; - //transform {'a', 'b', 's' || 'r'} => {0, 1, 2}; move_to_index = ((move & 0xFF) - 'a' + 9) / 10; - - //if move starts with 'rr', convert it to 'q' instead if (move >> 16 == 'r') move = (move & 0xFF) | (('r' - 1) << 8); - //transform {'p', 'q', 'r', 's'} => {3, 2, 1, 0}; - //p is the only family of fcts with less than 3 members, needs to be last. - //get the position and multiply by 3 to get the offset. move_to_index += (((move >> 8) & 0xFF) - 'p' - 3) * (-1) * 3; - return (move_to_index); } diff --git a/src/unit_tests/calc_insert_costs_test.c b/src/unit_tests/calc_insert_costs_test.c deleted file mode 100644 index 6585af4..0000000 --- a/src/unit_tests/calc_insert_costs_test.c +++ /dev/null @@ -1,48 +0,0 @@ -#include "../../include/push_swap.h" - -typedef struct s_test_insert_info -{ - t_stack_insert_info a_info; - t_stack_insert_info b_info; - int min_cost; - int curr_cost; - int min_delta_insert; - int curr_delta_insert; -} t_test_insert_info; - -int test_test_calc_insert_cost(void) -{ - int size_a, size_b, pos_a, pos_b; - - size_a = 5; - size_b = 5; - pos_a = 1; - pos_b = 4; - printf("size A\t: %d\nsize B\t: %d\npos_a\t: %d\npos_b\t: %d\n", size_a, size_b, pos_a, pos_b); - printf("dist_min==> %d\n", test_calc_insert_cost(pos_a, size_a, pos_b, size_b)); - size_a = 88; - size_b = 33; - pos_a = 35; - pos_b = 32; - printf("size A\t: %d\nsize B\t: %d\npos_a\t: %d\npos_b\t: %d\n", size_a, size_b, pos_a, pos_b); - printf("dist_min==> %d\n", test_calc_insert_cost(pos_a, size_a, pos_b, size_b)); - size_a = 67; - size_b = 33; - pos_a = 33; - pos_b = 31; - printf("size A\t: %d\nsize B\t: %d\npos_a\t: %d\npos_b\t: %d\n", size_a, size_b, pos_a, pos_b); - printf("dist_min==> %d\n", test_calc_insert_cost(pos_a, size_a, pos_b, size_b)); - size_a = 67; - size_b = 33; - pos_a = 63; - pos_b = 15; - printf("size A\t: %d\nsize B\t: %d\npos_a\t: %d\npos_b\t: %d\n", size_a, size_b, pos_a, pos_b); - printf("dist_min==> %d\n", test_calc_insert_cost(pos_a, size_a, pos_b, size_b)); - size_a = 67; - size_b = 33; - pos_a = 2; - pos_b = 32; - printf("size A\t: %d\nsize B\t: %d\npos_a\t: %d\npos_b\t: %d\n", size_a, size_b, pos_a, pos_b); - printf("dist_min==> %d\n", test_calc_insert_cost(pos_a, size_a, pos_b, size_b)); - return (0); -} diff --git a/src/utils.c b/src/utils.c deleted file mode 100644 index 4a31793..0000000 --- a/src/utils.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "push_swap.h" - -void *ft_safealloc(size_t size) -{ - void *mem_space; - - mem_space = malloc(size); - if (!mem_space) - { - write(STDERR_FILENO, "Malloc Error\n", 13); - exit (EXIT_FAILURE); - } - return (mem_space); -} diff --git a/tester100.sh b/tester100.sh deleted file mode 100755 index 37b35e0..0000000 --- a/tester100.sh +++ /dev/null @@ -1,91 +0,0 @@ -#! /bin/bash - -RED="\033[31m" -GREEN="\033[32m" -ENDCOLOR="\033[0m" - -NB_VALS=100 -MAX_NB_MOVES=0 -MAX_TEST_ID=0 -N=1.38 -# LIMIT=$(bc -l <<< "e($N*l($NB_VALS))") -LIMIT=700 -INT_LIMIT=${LIMIT%.1} -NB_CASES_ABOVE_LIMIT=0 -ITERATIONS=1 -SUM=0 -LOG_FILE=sore_thumbs_$NB_VALS.log -PS_TEMPFILE=tester_temp_$NB_VALS.log -PS_EXE=./push_swap_$NB_VALS - -echo -e "${GREEN}Testing for ${NB_VALS} values ${ENDCOLOR}" - - -for i in {1..1000} -do - export ARG=`ruby -e "puts (1..$NB_VALS).to_a.shuffle.join(' ')"` - $PS_EXE $ARG > $PS_TEMPFILE - NB_MOVES=$(< $PS_TEMPFILE wc -l | sed 's/ //g'); - if [ $(< $PS_TEMPFILE ./checker_Mac $ARG | grep -q KO) ]; - then - echo "Error!" - echo "Error!" >> $LOG_FILE - echo $ARG >> $LOG_FILE - echo "nb_moves:" >> $LOG_FILE - echo $NB_MOVES >> $LOG_FILE - break - fi - if [ $(echo "$NB_MOVES > $LIMIT" | bc) -eq 1 ]; - then - echo "Test #"$i":" $NB_MOVES "[$(cat $PS_TEMPFILE | ./checker_Mac $ARG)]" >> $LOG_FILE - echo $ARG >> $LOG_FILE - echo "" >> $LOG_FILE - let NB_CASES_ABOVE_LIMIT+=1; - fi - if [ $(echo $NB_MOVES ">" $MAX_NB_MOVES | bc) -eq 1 ]; - then - echo "Test #"$i":" $NB_MOVES "[$(cat $PS_TEMPFILE | ./checker_Mac $ARG)]" >> $LOG_FILE - echo "$ARG" >> $LOG_FILE - echo "" >> $LOG_FILE - MAX_NB_MOVES=$NB_MOVES; - MAX_TEST_ID=$i; - fi - - if [ $(echo $NB_MOVES "<" $LIMIT | bc) -eq 1 ]; - then - echo -e "${i} : ${GREEN}${NB_MOVES}${ENDCOLOR}" - else - echo -e "${i} : ${RED}${NB_MOVES}${ENDCOLOR}" - fi - let SUM+=$NB_MOVES; - let ITERATIONS+=1; - let i+=1; -done - -rm $PS_TEMPFILE -echo "Nb tests done:" $(($ITERATIONS - 1)) -if [ $(echo "$(($SUM / ($ITERATIONS - 1))) < ${LIMIT}" | bc) -eq 1 ]; -then - echo -e "${GREEN}Average nb of moves: $(($SUM / ($ITERATIONS - 1)))${ENDCOLOR}" -else - echo -e "${RED}Average nb of moves: $(($SUM / ($ITERATIONS - 1)))${ENDCOLOR}" -fi - -if [ $(echo $MAX_NB_MOVES "<" $LIMIT | bc) -eq 1 ]; -then - echo -e "${GREEN}Worst nb of moves: ${MAX_NB_MOVES}${ENDCOLOR}" -else - echo -e "${RED}Worst nb of moves: ${MAX_NB_MOVES}${ENDCOLOR}" -fi - -if [ $NB_CASES_ABOVE_LIMIT -eq 0 ]; -then - printf "${GREEN}%s %.0f %s %d\n${ENDCOLOR}" "Nb of moves above" $LIMIT ":" $NB_CASES_ABOVE_LIMIT -else - printf "${RED}%s %.0f %s %d\n${ENDCOLOR}" "Nb of moves above" $LIMIT ":" $NB_CASES_ABOVE_LIMIT -fi - -echo "Nb tests done:" $ITERATIONS >> $LOG_FILE -echo "Average nb of moves: $(($SUM / ($ITERATIONS - 1)))" >> $LOG_FILE -echo "(Test #"$MAX_TEST_ID") Worst nb of moves:" $MAX_NB_MOVES >> $LOG_FILE -printf "%s %.0f %s %d\n" "Nb of moves above" $LIMIT ":" $NB_CASES_ABOVE_LIMIT >> $LOG_FILE diff --git a/tester5.sh b/tester5.sh deleted file mode 100755 index eaa7dee..0000000 --- a/tester5.sh +++ /dev/null @@ -1,89 +0,0 @@ -#! /bin/bash - -RED="\033[31m" -GREEN="\033[32m" -ENDCOLOR="\033[0m" - -NB_VALS=5 -MAX_NB_MOVES=0 -MAX_TEST_ID=0 -N=1.38 -# LIMIT=$(bc -l <<< "e($N*l($NB_VALS))") -LIMIT=12 -INT_LIMIT=${LIMIT%.1} -NB_CASES_ABOVE_LIMIT=0 -ITERATIONS=1 -PERMUTATIONS_FILE=5_permutations_numbered.txt -LOG_FILE=sore_thumbs_$NB_VALS.log -PS_TEMPFILE=tester_temp_$NB_VALS.log - -echo -e "${GREEN}Testing for ${NB_VALS} values ${ENDCOLOR}" - -for i in {1..120} -do - export ARG=$(awk -F: -v i=$i 'NR==i{printf $2;}' $PERMUTATIONS_FILE) - ./push_swap $ARG > $PS_TEMPFILE - NB_MOVES=$(< $PS_TEMPFILE wc -l | sed 's/ //g'); - if [ $(< $PS_TEMPFILE ./checker_Mac $ARG | grep -q KO) ]; - then - echo "Error!" - echo "Error!" >> $LOG_FILE - echo $ARG >> $LOG_FILE - echo "nb_moves:" >> $LOG_FILE - echo $NB_MOVES >> $LOG_FILE - break - fi - if [ $(echo "$NB_MOVES >= $LIMIT" | bc) -eq 1 ]; - then - echo "Test #"$i":" $NB_MOVES "[$(cat $PS_TEMPFILE | ./checker_Mac $ARG)]" >> $LOG_FILE - echo $ARG >> $LOG_FILE - echo "" >> $LOG_FILE - let NB_CASES_ABOVE_LIMIT+=1; - fi - if [ $(echo $NB_MOVES ">" $MAX_NB_MOVES | bc) -eq 1 ]; - then - echo "Test #"$i":" $NB_MOVES "[$(cat $PS_TEMPFILE | ./checker_Mac $ARG)]" >> $LOG_FILE - echo "$ARG" >> $LOG_FILE - echo "" >> $LOG_FILE - MAX_NB_MOVES=$NB_MOVES; - MAX_TEST_ID=$i; - fi - - if [ $(< $PS_TEMPFILE ./checker_Mac $ARG | grep OK) ]; - then - echo -e "$ARG: ${GREEN}${NB_MOVES} "[$(cat $PS_TEMPFILE | ./checker_Mac $ARG)]"${ENDCOLOR}" - else - echo -e "$ARG: ${RED}${NB_MOVES} "[$(cat $PS_TEMPFILE | ./checker_Mac $ARG)]"${ENDCOLOR}" - fi - let SUM+=$NB_MOVES; - let ITERATIONS+=1; - let i+=1; -done - -rm $PS_TEMPFILE -echo "Nb tests done:" $(($ITERATIONS - 1)) -if [ $(echo "$(($SUM / ($ITERATIONS - 1))) < ${LIMIT}" | bc) -eq 1 ]; -then - echo -e "${GREEN}Average nb of moves: $(($SUM / ($ITERATIONS - 1)))${ENDCOLOR}" -else - echo -e "${RED}Average nb of moves: $(($SUM / ($ITERATIONS - 1)))${ENDCOLOR}" -fi - -if [ $(echo $MAX_NB_MOVES "<" $LIMIT | bc) -eq 1 ]; -then - echo -e "${GREEN}Worst nb of moves: ${MAX_NB_MOVES}${ENDCOLOR}" -else - echo -e "${RED}Worst nb of moves: ${MAX_NB_MOVES}${ENDCOLOR}" -fi - -if [ $NB_CASES_ABOVE_LIMIT -eq 0 ]; -then - printf "${GREEN}%s %.0f %s %d\n${ENDCOLOR}" "Nb of moves above" $LIMIT ":" $NB_CASES_ABOVE_LIMIT -else - printf "${RED}%s %.0f %s %d\n${ENDCOLOR}" "Nb of moves above" $LIMIT ":" $NB_CASES_ABOVE_LIMIT -fi - -echo "Nb tests done:" $ITERATIONS >> $LOG_FILE -echo "Average nb of moves: $(($SUM / ($ITERATIONS - 1)))" >> $LOG_FILE -echo "(Test #"$MAX_TEST_ID") Worst nb of moves:" $MAX_NB_MOVES >> $LOG_FILE -printf "%s %.0f %s %d\n" "Nb of moves above" $LIMIT ":" $NB_CASES_ABOVE_LIMIT >> $LOG_FILE diff --git a/tester500.sh b/tester500.sh deleted file mode 100755 index 682473e..0000000 --- a/tester500.sh +++ /dev/null @@ -1,91 +0,0 @@ -#! /bin/bash - -RED="\033[31m" -GREEN="\033[32m" -ENDCOLOR="\033[0m" - -NB_VALS=500 -MAX_NB_MOVES=0 -MAX_TEST_ID=0 -N=1.38 -# LIMIT=$(bc -l <<< "e($N*l($NB_VALS))") -LIMIT=5500 -INT_LIMIT=${LIMIT%.1} -NB_CASES_ABOVE_LIMIT=0 -ITERATIONS=1 -SUM=0 -LOG_FILE=sore_thumbs_$NB_VALS.log -PS_TEMPFILE=tester_temp_$NB_VALS.log -PS_EXE=./push_swap_$NB_VALS - -echo -e "${GREEN}Testing for ${NB_VALS} values ${ENDCOLOR}" - - -for i in {1..1000} -do - export ARG=`ruby -e "puts (1..$NB_VALS).to_a.shuffle.join(' ')"` - $PS_EXE $ARG > $PS_TEMPFILE - NB_MOVES=$(< $PS_TEMPFILE wc -l | sed 's/ //g'); - if [ $(< $PS_TEMPFILE ./checker_Mac $ARG | grep -q KO) ]; - then - echo "Error!" - echo "Error!" >> $LOG_FILE - echo $ARG >> $LOG_FILE - echo "nb_moves:" >> $LOG_FILE - echo $NB_MOVES >> $LOG_FILE - break - fi - if [ $(echo "$NB_MOVES > $LIMIT" | bc) -eq 1 ]; - then - echo "Test #"$i":" $NB_MOVES "[$(cat $PS_TEMPFILE | ./checker_Mac $ARG)]" >> $LOG_FILE - echo $ARG >> $LOG_FILE - echo "" >> $LOG_FILE - let NB_CASES_ABOVE_LIMIT+=1; - fi - if [ $(echo $NB_MOVES ">" $MAX_NB_MOVES | bc) -eq 1 ]; - then - echo "Test #"$i":" $NB_MOVES "[$(cat $PS_TEMPFILE | ./checker_Mac $ARG)]" >> $LOG_FILE - echo "$ARG" >> $LOG_FILE - echo "" >> $LOG_FILE - MAX_NB_MOVES=$NB_MOVES; - MAX_TEST_ID=$i; - fi - - if [ $(echo $NB_MOVES "<" $LIMIT | bc) -eq 1 ]; - then - echo -e "${i} : ${GREEN}${NB_MOVES}${ENDCOLOR}" - else - echo -e "${i} : ${RED}${NB_MOVES}${ENDCOLOR}" - fi - let SUM+=$NB_MOVES; - let ITERATIONS+=1; - let i+=1; -done - -rm $PS_TEMPFILE -echo "Nb tests done:" $(($ITERATIONS - 1)) -if [ $(echo "$(($SUM / ($ITERATIONS - 1))) < ${LIMIT}" | bc) -eq 1 ]; -then - echo -e "${GREEN}Average nb of moves: $(($SUM / ($ITERATIONS - 1)))${ENDCOLOR}" -else - echo -e "${RED}Average nb of moves: $(($SUM / ($ITERATIONS - 1)))${ENDCOLOR}" -fi - -if [ $(echo $MAX_NB_MOVES "<" $LIMIT | bc) -eq 1 ]; -then - echo -e "${GREEN}Worst nb of moves: ${MAX_NB_MOVES}${ENDCOLOR}" -else - echo -e "${RED}Worst nb of moves: ${MAX_NB_MOVES}${ENDCOLOR}" -fi - -if [ $NB_CASES_ABOVE_LIMIT -eq 0 ]; -then - printf "${GREEN}%s %.0f %s %d\n${ENDCOLOR}" "Nb of moves above" $LIMIT ":" $NB_CASES_ABOVE_LIMIT -else - printf "${RED}%s %.0f %s %d\n${ENDCOLOR}" "Nb of moves above" $LIMIT ":" $NB_CASES_ABOVE_LIMIT -fi - -echo "Nb tests done:" $ITERATIONS >> $LOG_FILE -echo "Average nb of moves: $(($SUM / ($ITERATIONS - 1)))" >> $LOG_FILE -echo "(Test #"$MAX_TEST_ID") Worst nb of moves:" $MAX_NB_MOVES >> $LOG_FILE -printf "%s %.0f %s %d\n" "Nb of moves above" $LIMIT ":" $NB_CASES_ABOVE_LIMIT >> $LOG_FILE