Guest User

Untitled

a guest
Jun 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5. class Point {
  6. final int row;
  7. final int col;
  8.  
  9. public Point(int row, int col) {
  10. this.row = row;
  11. this.col = col;
  12. }
  13. }
  14.  
  15. class FoundCharacterPoint {
  16. private final Character character;
  17. private final Point point;
  18.  
  19. public FoundCharacterPoint(Character character, Point point) {
  20. this.character = character;
  21. this.point = point;
  22. }
  23.  
  24. @Override
  25. public String toString() {
  26. return String.format("%s - (%s, %s)", character, point.row, point.col);
  27. }
  28. }
  29.  
  30. class FindPointsTaskEntry {
  31. private int pointsToFind;
  32. private LinkedList<Point> pointList;
  33.  
  34. public FindPointsTaskEntry(int pointsToFind) {
  35. this.pointsToFind = pointsToFind;
  36. this.pointList = new LinkedList<>();
  37. }
  38.  
  39. public int getCountToFind() {
  40. return pointsToFind;
  41. }
  42.  
  43. public void addPoint(Point point) {
  44. if (pointsToFind-- > 0) {
  45. pointList.add(point);
  46. }
  47. }
  48.  
  49. public Point consumePoint() {
  50. return pointList.pollFirst();
  51. }
  52. }
  53.  
  54. class FindPointsTask {
  55. private final Map<Character, FindPointsTaskEntry> taskData;
  56.  
  57. public static FindPointsTask createFromSpec(Map<Character, Integer> taskSpec) {
  58. Map<Character, FindPointsTaskEntry> taskData = new HashMap<>();
  59. for (Map.Entry<Character, Integer> taskSpecEntry : taskSpec.entrySet()) {
  60. taskData.put(taskSpecEntry.getKey(), new FindPointsTaskEntry(taskSpecEntry.getValue()));
  61. }
  62. return new FindPointsTask(taskData);
  63. }
  64.  
  65. private FindPointsTask(Map<Character, FindPointsTaskEntry> taskData) {
  66. this.taskData = taskData;
  67. }
  68.  
  69. public Integer getTotalPointsToFind() {
  70. int totalPointsToFind = 0;
  71. for (FindPointsTaskEntry entry : taskData.values()) {
  72. totalPointsToFind += entry.getCountToFind();
  73. }
  74. return totalPointsToFind;
  75. }
  76.  
  77. public boolean hasPointsToFind() {
  78. return getTotalPointsToFind() > 0;
  79. }
  80.  
  81. public void tryAddPoint(Character character, Point point) {
  82. Character charInLowerCase = Character.toLowerCase(character);
  83. if (taskData.containsKey(charInLowerCase)) {
  84. taskData.get(charInLowerCase).addPoint(point);
  85. }
  86. }
  87.  
  88. public List<FoundCharacterPoint> prepareForTargetString(String targetString) {
  89. List<FoundCharacterPoint> characterPoints = new ArrayList<>();
  90. for (Character character : targetString.toLowerCase().toCharArray()) {
  91. FindPointsTaskEntry entry = taskData.get(Character.toLowerCase(character));
  92. characterPoints.add(new FoundCharacterPoint(character, entry.consumePoint()));
  93. }
  94. return characterPoints;
  95. }
  96. }
  97.  
  98. interface TaskProcessor {
  99. void process(FindPointsTask findPointsTask);
  100. }
  101.  
  102. class TaskProcessorImpl implements TaskProcessor {
  103.  
  104. private final String filePath;
  105.  
  106. public TaskProcessorImpl(String filePath) {
  107. this.filePath = filePath;
  108. }
  109.  
  110. @Override
  111. public void process(FindPointsTask findPointsTask) {
  112. try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
  113.  
  114. // We don't really care about the matrix size
  115. reader.readLine();
  116.  
  117. int currentRow = 0;
  118. int currentCol = 0;
  119. String nextLine;
  120. while ((nextLine = reader.readLine()) != null && findPointsTask.hasPointsToFind()) {
  121. for (Character character : nextLine.toCharArray()) {
  122. findPointsTask.tryAddPoint(character, new Point(currentRow, currentCol++));
  123. }
  124. currentRow++;
  125. currentCol = 0;
  126. }
  127.  
  128. } catch (IOException e) {
  129. throw new RuntimeException(e);
  130. }
  131. }
  132. }
  133.  
  134. class TaskSpecFactory {
  135.  
  136. public static Map<Character, Integer> createTaskSpec(String targetString) {
  137. Map<Character, Integer> spec = new HashMap<>();
  138. for (Character ch : targetString.toLowerCase().toCharArray()) {
  139. if (spec.containsKey(ch)) {
  140. spec.put(ch, spec.get(ch) + 1);
  141. } else {
  142. spec.put(ch, 1);
  143. }
  144. }
  145.  
  146. return spec;
  147. }
  148. }
  149.  
  150. interface FindPointsTaskWriter {
  151. void write(FindPointsTask task);
  152. }
  153.  
  154. class FindPointsTaskFileWriter implements FindPointsTaskWriter {
  155.  
  156. private final String filePath;
  157. private final String targetString;
  158.  
  159. public FindPointsTaskFileWriter(String filePath, String targetString) {
  160. this.filePath = filePath;
  161. this.targetString = targetString;
  162. }
  163.  
  164. @Override
  165. public void write(FindPointsTask task) {
  166. try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
  167. if (!task.hasPointsToFind()) {
  168. for (FoundCharacterPoint fcp : task.prepareForTargetString(targetString)) {
  169. writer.write(fcp.toString());
  170. writer.newLine();
  171. }
  172. } else {
  173. writer.write("Impossible");
  174. }
  175. } catch (IOException e) {
  176. throw new RuntimeException(e);
  177. }
  178. }
  179. }
  180.  
  181. public class Main {
  182.  
  183. private static final String INPUT_PATH = "/Users/dbychkov/input.txt";
  184. private static final String OUTPUT_PATH = "/Users/dbychkov/output.txt";
  185. private static final String TARGET_STRING = "OneTwoTrip";
  186.  
  187. public static void main(String[] args) {
  188. FindPointsTask task = FindPointsTask.createFromSpec(TaskSpecFactory.createTaskSpec(TARGET_STRING));
  189. new TaskProcessorImpl(INPUT_PATH).process(task);
  190. new FindPointsTaskFileWriter(OUTPUT_PATH, TARGET_STRING).write(task);
  191. }
  192. }
Add Comment
Please, Sign In to add comment