<commit_before>package algorithms.simpletexteditor;

import java.io.FileNotFoundException;
import java.util.*;

public class Solution {

  private static StringBuilder result;
  private static StringBuilder sb;

  public static void main(String[] args) throws FileNotFoundException {
  Scanner scanner = new Scanner(System.in);
//    Scanner scanner = new Scanner(new FileInputStream("/home/webdude/Documents/Projects/HackerRank/input10.txt"));
    int linesOfInput = Integer.parseInt(scanner.nextLine());
    List<List<String>> commands = new ArrayList<>();

    for (int i = 0; i < linesOfInput; i++) {
      String[] commandArgs = scanner.nextLine().split("\\s+");
      if (commandArgs.length > 1) {
        commands.add(Arrays.asList(commandArgs[0], commandArgs[1]));
      } else {
        commands.add(Collections.singletonList(commandArgs[0]));
      }
    }

    printResult(executeCommands(commands));
  }

  private static void printResult(char[] result) {
    sb = new StringBuilder();

    for (char character : result) {
      sb.append(character);
      sb.append(System.lineSeparator());
    }

    System.out.println(sb.toString());
  }

  public static char[] executeCommands(List<List<String>> input) {
    result = new StringBuilder();

    Deque<Command> commandQueue = new LinkedList<>();

    for (List<String> strings : input) {
      Command command = parseCommand(strings);
      switch (command.getCommandCode()) {
        case Command.APPEND:
        case Command.DELETE:
          commandQueue.add(command);
          break;
        case Command.UNDO:
          commandQueue.pollLast();
          break;
        case Command.PRINT:
          renderState(commandQueue);
          print(command.commandArgument);
          break;
      }
    }

    return result.toString().toCharArray();
  }

  private static void renderState(Queue<Command> commandQueue) {
    sb = new StringBuilder();

    commandQueue.forEach(command -> {
      if (command.getCommandCode().equals(Command.APPEND)) {
        append(command.commandArgument);
      } else if (command.getCommandCode().equals(Command.DELETE)) {
        delete(command.commandArgument);
      }
    });
  }

  private static void append(String commandArgument) {
    sb.append(commandArgument);
  }

  private static void delete(String commandArgument) {
    sb.delete(sb.length() - Integer.parseInt(commandArgument), sb.length());
  }

  private static void print(String charToPrint) {
    result.append(sb.charAt(Integer.parseInt(charToPrint) - 1));
  }

  private static Command parseCommand(List<String> input) {
    return new Command(input.get(0), input.size() > 1 ? input.get(1) : null);
  }

  static class Command {
    static final String APPEND = "1";
    static final String DELETE = "2";
    static final String PRINT = "3";
    static final String UNDO = "4";

    private final String commandArgument;
    private final String commandCode;

    public Command(String commandCode, String commandArgument) {
      this.commandCode = commandCode;
      this.commandArgument = commandArgument;
    }

    public String getCommandCode() {
      return this.commandCode;
    }
  }<commit_msg>optimize the text editor a bit so more tests pass<commit_after>package algorithms.simpletexteditor;

import java.util.*;

public class Solution {

  private static StringBuilder result;
  private static StringBuilder sb;

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int linesOfInput = Integer.parseInt(scanner.nextLine());
    List<List<String>> commands = new ArrayList<>();

    for (int i = 0; i < linesOfInput; i++) {
      String[] commandArgs = scanner.nextLine().split("\\s+");
      if (commandArgs.length > 1) {
        commands.add(Arrays.asList(commandArgs[0], commandArgs[1]));
      } else {
        commands.add(Collections.singletonList(commandArgs[0]));
      }
    }

    printResult(executeCommands(commands));
  }

  private static void printResult(char[] result) {
    sb = new StringBuilder();

    for (char character : result) {
      sb.append(character);
      sb.append(System.lineSeparator());
    }

    System.out.println(sb.toString());
  }

  public static char[] executeCommands(List<List<String>> commands) {
    result = new StringBuilder();

    Deque<String> state = new LinkedList<>();

    state.push("");

    for (List<String> strings : commands) {
      Command command = parseCommand(strings);
      switch (command.getCommandCode()) {
        case Command.APPEND:
        case Command.DELETE:
          state.push(renderState(command, state.peek()));
          break;
        case Command.UNDO:
          state.pop();
          break;
        case Command.PRINT:
          print(Objects.requireNonNull(state.peek()), command.commandArgument);
          break;
      }
    }

    return result.toString().toCharArray();
  }

  private static String renderState(Command command, String currentState) {
    sb = new StringBuilder(currentState);

    final String commandCode = command.getCommandCode();
    final String commandArgument = command.commandArgument;
    if (commandCode.equals(Command.APPEND)) {
      append(commandArgument);
    } else if (commandCode.equals(Command.DELETE)) {
      delete(commandArgument);
    }

    return sb.toString();
  }

  private static void append(String commandArgument) {
    sb.append(commandArgument);
  }

  private static void delete(String commandArgument) {
    sb.delete(sb.length() - Integer.parseInt(commandArgument), sb.length());
  }

  private static void print(String currentState, String charToPrint) {
    result.append(currentState.charAt(Integer.parseInt(charToPrint) - 1));
  }

  private static Command parseCommand(List<String> input) {
    return new Command(input.get(0), input.size() > 1 ? input.get(1) : null);
  }

  static class Command {
    static final String APPEND = "1";
    static final String DELETE = "2";
    static final String PRINT = "3";
    static final String UNDO = "4";

    private final String commandArgument;
    private final String commandCode;

    public Command(String commandCode, String commandArgument) {
      this.commandCode = commandCode;
      this.commandArgument = commandArgument;
    }

    public String getCommandCode() {
      return this.commandCode;
    }
  }
{'fraction_non_alphanumeric': 0.11729465552577645, 'fraction_numerical': 0.004414314992905565, 'mean_word_length': 3.2124833997343956, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6033523', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1727, 'n_words': 425}