<commit_before><commit_msg>Create Formatter for "slice" output<commit_after>package com.google.hashcode.utils;


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import com.google.hashcode.entity.Cell;
import com.google.hashcode.entity.Ingredient;

/**
 * 
 * @author Tuzhanskyi (jarc0der)
 *
 */

public class Formatter {
	private List<List<Cell>> cellList;
	private final Comparator<Cell> comp = ((thisCell, thatCell) -> {
		if(thisCell.x != thatCell.x){
			return Integer.compare(thisCell.x, thatCell.x);
		}else{
			return Integer.compare(thisCell.y, thatCell.y);
		}
	});

	public Formatter(List<List<Cell>> list) {
		this.cellList = list;
	}

	public Formatter() {
		this(hardInit());
		// this.cellList = hardInit();
	}

	public void writeResultsToFile(String fileName) throws IOException {

		try (BufferedWriter out = new BufferedWriter(new FileWriter(fileName))) {
			out.write("" + cellList.size());
			out.newLine();
			for (List<Cell> slice : cellList) {
				Cell maxCell = slice.stream().max(comp).get();
				Cell minCell = slice.stream().min(comp).get();
				
				String coords = getCoordsLineForSlice(minCell, maxCell);
				out.write(coords);
				out.newLine();
			}
			
			//check: autoflush?
			out.flush();
		}

	}

	private String getCoordsLineForSlice(Cell min, Cell max) {
		StringBuilder sb = new StringBuilder();
		sb.append(min.y).append(" " + min.x).append(" " + max.y).append(" " + max.x);
		return sb.toString();
	}

	private static List<List<Cell>> hardInit() {
		List<List<Cell>> resultList = new ArrayList<>();
		List<Cell> slice0 = new ArrayList<>();
		List<Cell> slice1 = new ArrayList<>();
		List<Cell> slice2 = new ArrayList<>();

		slice0.add(new Cell(0, 0, Ingredient.TOMATO));
		slice0.add(new Cell(0, 1, Ingredient.TOMATO));
		slice0.add(new Cell(0, 2, Ingredient.TOMATO));
		slice0.add(new Cell(1, 0, Ingredient.TOMATO));
		slice0.add(new Cell(1, 1, Ingredient.MUSHROOM));
		slice0.add(new Cell(1, 2, Ingredient.TOMATO));

		slice1.add(new Cell(2, 0, Ingredient.TOMATO));
		slice1.add(new Cell(2, 1, Ingredient.MUSHROOM));
		slice1.add(new Cell(2, 2, Ingredient.TOMATO));

		slice2.add(new Cell(3, 0, Ingredient.TOMATO));
		slice2.add(new Cell(3, 1, Ingredient.MUSHROOM));
		slice2.add(new Cell(3, 2, Ingredient.TOMATO));
		slice2.add(new Cell(4, 0, Ingredient.TOMATO));
		slice2.add(new Cell(4, 1, Ingredient.TOMATO));
		slice2.add(new Cell(4, 2, Ingredient.TOMATO));

		resultList.add(slice0);
		resultList.add(slice1);
		resultList.add(slice2);

		return resultList;
	}
}
{'fraction_non_alphanumeric': 0.16261398176291794, 'fraction_numerical': 0.019756838905775075, 'mean_word_length': 4.395491803278689, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, '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': '932263', 'n_tokens_mistral': 1067, 'n_tokens_neox': 982, 'n_words': 191}