ziobrowskyy

AOC 3.1

Dec 4th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. static void task5() {
  2. // String[] lines = openFile(new File("res/i4.txt"));
  3. List<String[]> lines = Arrays.stream(openFile(new File("res/i41.txt"))).map(x -> x.split(",")).collect(Collectors.toList());
  4.  
  5. List<Vector2> line1 = new ArrayList<>(lines.get(0).length-1);
  6. Vector2 pos1 = new Vector2(0,0);
  7. for(int i = 0; i < lines.get(0).length; i++) {
  8. String s = lines.get(0)[i];
  9. String dir = s.substring(0,1);
  10. int dist = Integer.parseInt(s.substring(1));
  11. switch (dir) {
  12. case "R":
  13. pos1.add(dist, 0);
  14. break;
  15. case "L":
  16. pos1.add(-dist, 0);
  17. break;
  18. case "U":
  19. pos1.add(0, dist);
  20. break;
  21. case "D":
  22. pos1.add(0, -dist);
  23. break;
  24. }
  25. line1.add(pos1.copy());
  26. }
  27. List<Vector2> intersections = new ArrayList<>();
  28. Vector2 line2a = new Vector2(0,0);
  29. for(int i = 0; i < lines.get(1).length; i++) {
  30. String s = lines.get(1)[i];
  31. String dir = s.substring(0,1);
  32. int dist = Integer.parseInt(s.substring(1));
  33. Vector2 line2b = line2a.copy();
  34. switch (dir) {
  35. case "R":
  36. line2b.add(dist, 0);
  37. break;
  38. case "L":
  39. line2b.add(-dist, 0);
  40. break;
  41. case "U":
  42. line2b.add(0, dist);
  43. break;
  44. case "D":
  45. line2b.add(0, -dist);
  46. break;
  47. }
  48. for(int j = 0; j < line1.size() -1; j++) {
  49. Vector2 line1a = line1.get(j);
  50. Vector2 line1b = line1.get(j+1);
  51. boolean intersectionFound = false;
  52. if(dir.matches("[UD]") && line1a.getY() == line1b.getY()) { //horizontal
  53. // System.out.println("LINE 1");
  54. // line1a.show();
  55. // line1b.show();
  56. // System.out.println("LINE2");
  57. // line2a.show();
  58. // line2b.show();
  59. int y1 = line1a.getY(), x2 = line2a.getX();
  60. int x1min = Math.min(line1a.getX(), line1b.getX());
  61. int x1max = Math.max(line1a.getX(), line1b.getX());
  62. int y2min = Math.min(line2a.getY(), line2b.getY());
  63. int y2max = Math.max(line2a.getY(), line2b.getY());
  64. if(y1 > y2min && y1 < y2max && x1min < x2 && x1max > x2) {
  65. intersectionFound = true;
  66. Vector2 intersection = new Vector2(line2a.getX(), line1a.getY());
  67. intersections.add(intersection);
  68. }
  69.  
  70.  
  71. } else if(dir.matches("[LR]") && line1a.getX() == line1b.getX()){ //vertical
  72. int x1 = line1a.getX(), y2 = line2a.getY();
  73. int y1min = Math.min(line1a.getY(), line1b.getY());
  74. int y1max = Math.max(line1a.getY(), line1b.getY());
  75. int x2min = Math.min(line2a.getX(), line2b.getX());
  76. int x2max = Math.max(line2a.getX(), line2b.getX());
  77. if(x1 > x2min && x1 < x2max && y1min < y2 && y1max > y2) {
  78. intersectionFound = true;
  79. Vector2 intersection = new Vector2(line1a.getX(), line2a.getY());
  80. intersections.add(intersection);
  81. }
  82. }
  83. }
  84. line2a = line2b;
  85. }
  86. int min = Integer.MAX_VALUE;
  87. System.out.println("intersections found:");
  88. for(Vector2 v : intersections) {
  89. v.show();
  90. min = Math.min(min, v.manhattanLength());
  91. }
  92. System.out.println("shortest distance from intersection is:");
  93. System.out.println(min);
  94. // for(Vector2 v : line1)
  95. // System.out.println(v.getX() + ", " + v.getY());
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment