Source code for submission s1280

Go to diff to previous submission

Ants.java

  1.  
  2. import java.io.BufferedReader;
  3.  
  4. /*
  5.  * To change this template, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Scanner;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16.  
  17. /**
  18.  *
  19.  * @author cteam021
  20.  */
  21. public class Ants {
  22.  
  23. private Klada klada;
  24.  
  25. public static void main(String[] args) {
  26. try {
  27. new Ants();
  28. } catch (Exception e) {
  29.  
  30. e.printStackTrace();
  31. }
  32. }
  33.  
  34. public Ants() throws IOException {
  35.  
  36. String line = reader.readLine();
  37. String length = line.substring(0, line.indexOf(' '));
  38. Integer lengthKlada = Integer.parseInt(length);
  39.  
  40. Integer numAnts = Integer.parseInt(line.substring(line.indexOf(' ') + 1));
  41.  
  42. klada = new Klada(lengthKlada);
  43.  
  44.  
  45. for (int i = 0; i < numAnts; i++) {
  46. line = reader.readLine();
  47. int pos = Integer.parseInt(line.substring(0, line.indexOf(' ')));
  48. String or = line.substring(line.indexOf(' ') + 1);
  49. if (or.equals("R")) {
  50. klada.addAnt(new Ant(pos, true));
  51. } else {
  52. klada.addAnt(new Ant(pos, false));
  53. }
  54. }
  55.  
  56. klada.sort();
  57. // System.out.println("DOME CALLED");
  58. doMe();
  59.  
  60.  
  61.  
  62. }
  63.  
  64. public void doMe() {
  65. String prefix = "The last ant will fall down in ";
  66. String suffix = " seconds - started at ";
  67. boolean printed = false;
  68. while (klada.getLength() > 1) {
  69. //find first collision
  70. int minIndex = -1;
  71. double minDiff = -1;
  72. // System.out.println("BEFORE CYCLE");
  73. for (int i = 0; i < klada.getLength() - 1; i++) {
  74. // System.out.println(" KLADA LENGTH: " + klada.getLength());
  75. Ant a1 = klada.get(i);
  76. Ant a2 = klada.get(i + 1);
  77. // System.out.println(" after init KLADA LENGTH: " + klada.getLength());
  78. if (collide(a1, a2)) {
  79.  
  80. double diff = Math.abs(a1.getPosition() - a2.getPosition());
  81. // System.out.println("diff je :" + diff);
  82. if ((minDiff == -1 || diff < minDiff) && diff > 0) {
  83. minDiff = diff;
  84. minIndex = i;
  85. }
  86. }
  87. }
  88. double maxDistanceLeft = -1;
  89. double maxDistanceRight = -1;
  90.  
  91. Ant corrAntLeft = null;
  92. Ant corrAntRight = null;
  93. // System.out.println("MIN DIFF JE : " + minDiff);
  94. if (minDiff == -1) {
  95. // System.out.println("MIN DIFF JE -1");
  96. for (Ant a : klada.getAnts()) {
  97.  
  98.  
  99.  
  100. if (a.isIsRight()) {
  101. double maxLocal = (klada.getInitLength() - a.getPosition());
  102. if (maxDistanceRight == -1 || maxLocal > maxDistanceRight) {
  103. corrAntRight = a;
  104. maxDistanceRight = maxLocal;
  105. }
  106.  
  107. } else {
  108. double maxLocal = a.getPosition();
  109. if (maxDistanceLeft == -1 || maxLocal > maxDistanceLeft) {
  110. corrAntLeft = a;
  111. maxDistanceLeft = maxLocal;
  112. }
  113. }
  114. }
  115.  
  116. if (maxDistanceLeft > maxDistanceRight) {
  117. System.out.println(prefix + (int) (klada.getSeconds() + maxDistanceLeft) +
  118. suffix +
  119. corrAntLeft.getStartingPosition() + ".");
  120. } else if (maxDistanceLeft < maxDistanceRight) {
  121. System.out.println(prefix + (int) (klada.getSeconds() + maxDistanceRight) + suffix +
  122. corrAntRight.getStartingPosition() + ".");
  123. } else {
  124. System.out.println(prefix + (int) (klada.getSeconds() + maxDistanceRight) +
  125. suffix +
  126. corrAntLeft.getStartingPosition() + " and " + corrAntRight.getStartingPosition() + ".");
  127. }
  128. printed = true;
  129. break;
  130. }
  131.  
  132.  
  133.  
  134. double collideIn = minDiff / 2.0;
  135. //shift all ants
  136. klada.shiftAnts(collideIn);
  137.  
  138. //turn all colliding ants
  139. klada.turnColliding();
  140. klada.removeFallen();
  141.  
  142. }
  143. Ant a = klada.get(0);
  144. double maxLocal;
  145. if (a.isIsRight()) {
  146. maxLocal = (klada.getInitLength() - a.getPosition());
  147. } else {
  148. maxLocal = a.getPosition();
  149.  
  150.  
  151. }
  152. if (!printed) {
  153. System.out.println(prefix + (int) (klada.getSeconds() + maxLocal) +
  154. suffix +
  155. a.getStartingPosition() + ".");
  156. }
  157. }
  158.  
  159. public boolean collide(Ant a1, Ant a2) {
  160. if (a1.isIsRight() && !a2.isIsRight()) {
  161. return true;
  162. }
  163. return false;
  164. }
  165. }
  166.  
  167. class Klada {
  168.  
  169. private double seconds = 0;
  170. private List<Ant> ants = new ArrayList<Ant>();
  171. private Integer length;
  172.  
  173. public Integer getInitLength() {
  174. return length;
  175. }
  176.  
  177. public Klada(Integer length) {
  178. this.length = length;
  179. }
  180.  
  181. public void turnColliding() {
  182. for (int i = 0; i < ants.size() - 1; i++) {
  183. Ant a1 = ants.get(i);
  184. Ant a2 = ants.get(i + 1);
  185. if (a1.getPosition() == a2.getPosition()) {
  186. a1.setIsRight(!a1.isIsRight());
  187. a2.setIsRight(!a2.isIsRight());
  188. }
  189. }
  190. }
  191.  
  192. public void sort() {
  193. for (int i = 0; i < ants.size() - 1; i++) {
  194. for (int j = 0; j < ants.size() - 1 - i; j++) {
  195. if (ants.get(j).getPosition() > ants.get(j + 1).getPosition()) {
  196. Ant a = ants.get(j);
  197. ants.set(j, ants.get(j + 1));
  198. ants.set(j + 1, a);
  199. }
  200. }
  201. }
  202.  
  203. }
  204.  
  205. public void removeFallen() {
  206. List<Ant> newAnts = new ArrayList<Ant>();
  207. for (Ant a : ants) {
  208. if (a.getPosition() >= 0 && a.getPosition() <= length) {
  209. newAnts.add(a);
  210. }
  211. }
  212. ants = newAnts;
  213. }
  214.  
  215. public void shiftAnts(double timeShiftFor) {
  216. seconds += timeShiftFor;
  217. for (Ant a : ants) {
  218. if (a.isIsRight()) {
  219. //shift right
  220. a.setPosition(a.getPosition() + timeShiftFor);
  221. } else {
  222. a.setPosition(a.getPosition() - timeShiftFor);
  223. }
  224. }
  225. }
  226.  
  227. public void addAnt(Ant ant) {
  228. ants.add(ant);
  229. }
  230.  
  231. public List<Ant> getAnts() {
  232. return ants;
  233. }
  234.  
  235. public Ant get(int i) {
  236. return ants.get(i);
  237. }
  238.  
  239. public void setAnts(List<Ant> ants) {
  240. this.ants = ants;
  241. }
  242.  
  243. public Integer getLength() {
  244. return ants.size();
  245. }
  246.  
  247. public void setLength(Integer length) {
  248. this.length = length;
  249. }
  250.  
  251. public double getSeconds() {
  252. return seconds;
  253. }
  254.  
  255. public void setSeconds(double seconds) {
  256. this.seconds = seconds;
  257. }
  258. }
  259.  
  260. class Ant {
  261.  
  262. private double position;
  263. private Integer startingPosition;
  264. //true= right; left= false
  265. private boolean isRight;
  266.  
  267. public Ant(Integer position, boolean isRight) {
  268. this.position = position;
  269. this.isRight = isRight;
  270. this.startingPosition = position;
  271. }
  272.  
  273. public Integer getStartingPosition() {
  274. return startingPosition;
  275. }
  276.  
  277. public void setStartingPosition(Integer startingPosition) {
  278. this.startingPosition = startingPosition;
  279. }
  280.  
  281. public boolean isIsRight() {
  282. return isRight;
  283. }
  284.  
  285. public void setIsRight(boolean isRight) {
  286. this.isRight = isRight;
  287. }
  288.  
  289. public double getPosition() {
  290. return position;
  291. }
  292.  
  293. public void setPosition(double position) {
  294. this.position = position;
  295. }
  296. }
  297.  

Diff to submission s1177

Ants.java

--- c4.s1177.cteam021.ants.java.0.Ants.java
+++ c4.s1280.cteam021.ants.java.0.Ants.java
@@ -27,5 +27,5 @@
             new Ants();
         } catch (Exception e) {
-            System.out.println("Exc");
+
             e.printStackTrace();
         }
@@ -56,4 +56,5 @@
 
         klada.sort();
+//        System.out.println("DOME CALLED");
         doMe();
 
@@ -70,9 +71,14 @@
             int minIndex = -1;
             double minDiff = -1;
+//            System.out.println("BEFORE CYCLE");
             for (int i = 0; i < klada.getLength() - 1; i++) {
+//                System.out.println(" KLADA LENGTH: " + klada.getLength());
                 Ant a1 = klada.get(i);
                 Ant a2 = klada.get(i + 1);
+//                System.out.println(" after init KLADA LENGTH: " + klada.getLength());
                 if (collide(a1, a2)) {
+                    
                     double diff = Math.abs(a1.getPosition() - a2.getPosition());
+//                    System.out.println("diff je :" + diff);
                     if ((minDiff == -1 || diff < minDiff) && diff > 0) {
                         minDiff = diff;
@@ -83,9 +89,10 @@
             double maxDistanceLeft = -1;
             double maxDistanceRight = -1;
-
+            
             Ant corrAntLeft = null;
             Ant corrAntRight = null;
+//            System.out.println("MIN DIFF JE : " + minDiff);
             if (minDiff == -1) {
-
+//                System.out.println("MIN DIFF JE -1");
                 for (Ant a : klada.getAnts()) {
 
@@ -152,5 +159,5 @@
 
     public boolean collide(Ant a1, Ant a2) {
-        if (a1.isIsRight() != a2.isIsRight()) {
+        if (a1.isIsRight() && !a2.isIsRight()) {
             return true;
         }