|
| 1 | +package com.google.hashcode.utils; |
| 2 | + |
| 3 | + |
| 4 | +import java.io.BufferedWriter; |
| 5 | +import java.io.FileWriter; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Comparator; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import com.google.hashcode.entity.Cell; |
| 12 | +import com.google.hashcode.entity.Ingredient; |
| 13 | + |
| 14 | +/** |
| 15 | + * |
| 16 | + * @author Tuzhanskyi (jarc0der) |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +public class Formatter { |
| 21 | + private List<List<Cell>> cellList; |
| 22 | + private final Comparator<Cell> comp = ((thisCell, thatCell) -> { |
| 23 | + if(thisCell.x != thatCell.x){ |
| 24 | + return Integer.compare(thisCell.x, thatCell.x); |
| 25 | + }else{ |
| 26 | + return Integer.compare(thisCell.y, thatCell.y); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + public Formatter(List<List<Cell>> list) { |
| 31 | + this.cellList = list; |
| 32 | + } |
| 33 | + |
| 34 | + public Formatter() { |
| 35 | + this(hardInit()); |
| 36 | + // this.cellList = hardInit(); |
| 37 | + } |
| 38 | + |
| 39 | + public void writeResultsToFile(String fileName) throws IOException { |
| 40 | + |
| 41 | + try (BufferedWriter out = new BufferedWriter(new FileWriter(fileName))) { |
| 42 | + out.write("" + cellList.size()); |
| 43 | + out.newLine(); |
| 44 | + for (List<Cell> slice : cellList) { |
| 45 | + Cell maxCell = slice.stream().max(comp).get(); |
| 46 | + Cell minCell = slice.stream().min(comp).get(); |
| 47 | + |
| 48 | + String coords = getCoordsLineForSlice(minCell, maxCell); |
| 49 | + out.write(coords); |
| 50 | + out.newLine(); |
| 51 | + } |
| 52 | + |
| 53 | + //check: autoflush? |
| 54 | + out.flush(); |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + private String getCoordsLineForSlice(Cell min, Cell max) { |
| 60 | + StringBuilder sb = new StringBuilder(); |
| 61 | + sb.append(min.y).append(" " + min.x).append(" " + max.y).append(" " + max.x); |
| 62 | + return sb.toString(); |
| 63 | + } |
| 64 | + |
| 65 | + private static List<List<Cell>> hardInit() { |
| 66 | + List<List<Cell>> resultList = new ArrayList<>(); |
| 67 | + List<Cell> slice0 = new ArrayList<>(); |
| 68 | + List<Cell> slice1 = new ArrayList<>(); |
| 69 | + List<Cell> slice2 = new ArrayList<>(); |
| 70 | + |
| 71 | + slice0.add(new Cell(0, 0, Ingredient.TOMATO)); |
| 72 | + slice0.add(new Cell(0, 1, Ingredient.TOMATO)); |
| 73 | + slice0.add(new Cell(0, 2, Ingredient.TOMATO)); |
| 74 | + slice0.add(new Cell(1, 0, Ingredient.TOMATO)); |
| 75 | + slice0.add(new Cell(1, 1, Ingredient.MUSHROOM)); |
| 76 | + slice0.add(new Cell(1, 2, Ingredient.TOMATO)); |
| 77 | + |
| 78 | + slice1.add(new Cell(2, 0, Ingredient.TOMATO)); |
| 79 | + slice1.add(new Cell(2, 1, Ingredient.MUSHROOM)); |
| 80 | + slice1.add(new Cell(2, 2, Ingredient.TOMATO)); |
| 81 | + |
| 82 | + slice2.add(new Cell(3, 0, Ingredient.TOMATO)); |
| 83 | + slice2.add(new Cell(3, 1, Ingredient.MUSHROOM)); |
| 84 | + slice2.add(new Cell(3, 2, Ingredient.TOMATO)); |
| 85 | + slice2.add(new Cell(4, 0, Ingredient.TOMATO)); |
| 86 | + slice2.add(new Cell(4, 1, Ingredient.TOMATO)); |
| 87 | + slice2.add(new Cell(4, 2, Ingredient.TOMATO)); |
| 88 | + |
| 89 | + resultList.add(slice0); |
| 90 | + resultList.add(slice1); |
| 91 | + resultList.add(slice2); |
| 92 | + |
| 93 | + return resultList; |
| 94 | + } |
| 95 | +} |
0 commit comments