/* ************************************************************************** */ /* */ /* ::: o_ :::::: ::: */ /* checker.c :+: / :+::+: :+: */ /* +:+ > +:++:+ +:+ */ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/11/24 15:05:06 by whaffman #+#+# #+#+# #+# #+# | */ /* Updated: 2024/11/24 15:05:43 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ #include "push_swap.h" #include #include #include int execute_move(const char *move, t_state *state) { const t_move moves[11] = {&sa, &sb, &ss, &pa, &pb, \ &ra, &rb, &rr, &rra, &rrb, &rrr}; const char *moves_names[] = {"sa\n", "sb\n", "ss\n", \ "pa\n", "pb\n", \ "ra\n", "rb\n", "rr\n", \ "rra\n", "rrb\n", "rrr\n"}; int i; i = 0; while (i < 11) { if (ft_strcmp(move, moves_names[i]) == 0) { moves[i](state, 1); return (1) ; } i++; } return (0); } int read_moves(t_state *state) { char *move; int ret; move = get_next_line(STDIN_FILENO); while (move) { ret = execute_move(move, state); free(move); if (!ret) return (0); move = get_next_line(STDIN_FILENO); } free(move); return (1); } int main(int argc, char *argv[]) { int size; t_state *state; if (!initialise_state(&state)) return (ft_putstr_fd("Error\n", STDERR_FILENO), 1); if (argc == 1) return (free_state(state), 1); while (argc-- > 1) { if (!new_element(&(state->a), argv[argc])) return (error(state)); } if (has_duplicates(state->a)) return (error(state)); size = ft_lstsize(state->a); if (!read_moves(state)) return (error(state)); if (ft_lstsize(state->a) == size && (size == 0 || is_sorted(state->a))) return (free_state(state), ft_printf("OK\n")); return (free_state(state), ft_printf("KO\n")); }