Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
2.7 kB
4
Indexable
Never
package Dreampad;

import java.util.Scanner;

public class Solution {

	static final int INIT_METHOD = 0;
	static final int WRITE_METHOD = 1;
	static final int MOVE_CURSOR_METHOD = 2;
	static final int UNDO_METHOD = 3;
	static final int REDO_METHOD = 4;
	static final int SHOW_METHOD = 5;

	public static void main(String[] args) throws Exception {
		System.setIn(new java.io.FileInputStream(".\\src\\Dreampad\\input.txt"));
		Scanner scanner = new Scanner(System.in);
		UserSolution userSolution = new UserSolution();

		int tests = scanner.nextInt();
		int totalActualPoints = 0;
		int totalUserPoints = 0;
		for (int i = 0; i < tests; i++) {
			int actualPoints = 0;
			int userPoints = 0;

			int numberOfMethodCalls = scanner.nextInt();
			for (int j = 0; j < numberOfMethodCalls; j++) {
				int methodName = scanner.nextInt();

				switch (methodName) {
				case INIT_METHOD: {
					char[] str = scanner.nextLine().substring(1).toCharArray();
					userSolution.init(str);
				}
					break;
				case WRITE_METHOD: {
					char[] charArray = scanner.next().toCharArray();
					for (int k = 0; k < charArray.length; k++) {
						char ch = charArray[k];
						if (ch == '\\') {
							k++;
							userSolution.write(getChar(charArray[k]));
						} else {
							userSolution.write(ch);
						}
					}
				}
					break;
				case MOVE_CURSOR_METHOD: {
					int pos = scanner.nextInt();
					userSolution.moveCursor(pos);
				}
					break;
				case UNDO_METHOD: {
					userSolution.undo();
				}
					break;
				case REDO_METHOD: {
					userSolution.redo();
				}
					break;
				case SHOW_METHOD: {
					char[] answer = scanner.nextLine().substring(1).toCharArray();
					char[] userAnswer = userSolution.show();
					actualPoints += answer.length;
					userPoints += compare(answer, userAnswer);
				}
					break;
				}
			}

			System.out.println("Test-" + (i + 1) + " points: " + userPoints + " of " + actualPoints);
			totalUserPoints += userPoints;
			totalActualPoints += actualPoints;
		}
		System.out.println("-----------------------------");
		System.out.println("Total points: " + totalUserPoints + " of " + totalActualPoints);

		scanner.close();
	}

	static int compare(char[] str1, char[] str2) {
		int points = 0;

		for (int i = 0; i < str1.length; i++) {
			if (str1[i] == str2[i]) {
				points++;
			} else {
				break;
			}
		}

		return points;
	}

	static char getChar(char ch) {
		if (ch == 'b') {
			return '\b';
		}
		if (ch == 'n') {
			return '\n';
		}
		if (ch == 's') {
			return ' ';
		} else {
			return ' ';
		}
	}

}