import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class Soup {
	private int size;
	private char[][] matriz;
	private ArrayList<String> words;

	public Soup(String file) {

		try {
			Scanner sc = new Scanner(new File(file));
			String l = sc.nextLine();
			size = l.length();
			
			matriz = new char[size][size];

			for (int row = 0; row < size; row++) {
				for (int col = 0; col < size; col++) {
					char c = line.charAt(col);
					if (c < 'A' || c > 'Z') {
						System.err.println("Puzzle characters need to be uppercase letters");
						System.exit(1);
					}
					matrix[row][col] = c;
				}
				if (row < (size - 1)) {
					line = sc.nextLine();
				}
			}

			words = new ArrayList<String>();

			while (sc.hasNextLine()) {
				line = sc.nextLine();
				String[] list = line.split("\\W+");
				for (int i = 0; i < list.length; i++) {
					if ((list[i].matches("^[a-zA-Z]*$")) && list[i].length() >= 4) {
						words.add(list[i].toUpperCase());
					}
				}
			}

			for (int i = 0; i < words.size(); i++) {
				for (int j = 0; j < words.size(); j++) {
					if (i != j && words.get(i).contains(words.get(j))) {
						System.err.println("Redundant words: " + words.get(i) + ", " + words.get(j));
						System.exit(1);
					}
				}
			}
			sc.close();

		} catch (Exception e) {
			System.err.format("Exception occurred trying to read '%s'.", file);
			e.printStackTrace();
			return;
		}
	}

	public int getSize() {
		return size;
	}

	public char[][] getMatriz() {
		return matriz;
	}

	public ArrayList<String> getWords() {
		return words;
	}

	public ArrayList<String> reverse(ArrayList<String> arr) {
		ArrayList<String> reversed = new ArrayList<String>();
		for (String s : arr) {
			reversed.add(new StringBuilder(s).reverse().toString());
		}
		return reversed;
	}

	public char[][] reverseLines() {
		char[][] matriz_r = new char[size][size];

		for (int row = 0; row < size; row++) {
			String row_r = new StringBuilder(String.valueOf(matrix[row])).reverse().toString();
			matriz_r[row] = row_r.toCharArray();
		}
		return matriz_r;
	}

	private ArrayList<String> getDiagonal(char[][] mat) {
		ArrayList<String> diags = new ArrayList<String>();

		int row = 0;
		int col;

		while (row < mat.length) {
			col = 0;
			int rowTemp = row;
			String d = "";
			while (rowTemp >= 0) {
				d = d + mat[rowTemp][col];
				rowTemp--;
				col++;
			}
			diags.add(d);
			row++;
		}

		col = 1;

		while (col < mat.length) {
			int colTemp = col;
			row = mat.length - 1;
			String d = "";
			while (colTemp <= mat.length - 1) {
				d = d + mat[row][colTemp];
				row--;
				colTemp++;
			}
			diags.add(d);
			col++;
		}
		return diags;
	}

	public ArrayList<String> getHorizontals() {
		ArrayList<String> horizontals = new ArrayList<String>();
		for (int i = 0; i < matriz.length; i++) {
			horizontals.add(String.valueOf(matrix[i]));
		}
		return horizontals;
	}

	public ArrayList<String> getVerticals() {
		ArrayList<String> verticals = new ArrayList<String>();
		for (int row = 0; row < matriz.length; row++) {
			String v = "";
			for (int col = 0; col < matriz.length; col++) {
				v = v + matriz[col][row];
			}
			verticals.add(v);
		}
		return verticals;
	}

	public ArrayList<String> getDiagonal_UL() {
		ArrayList<String> diags = getDiagonal(reverseLines());
		return diags;
	}
	
	public ArrayList<String> getDiagonal_UR() {
		ArrayList<String> diags = getDiagonal(matrix);
		return diags;
	}


}