Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void task5() {
- // String[] lines = openFile(new File("res/i4.txt"));
- List<String[]> lines = Arrays.stream(openFile(new File("res/i41.txt"))).map(x -> x.split(",")).collect(Collectors.toList());
- List<Vector2> line1 = new ArrayList<>(lines.get(0).length-1);
- Vector2 pos1 = new Vector2(0,0);
- for(int i = 0; i < lines.get(0).length; i++) {
- String s = lines.get(0)[i];
- String dir = s.substring(0,1);
- int dist = Integer.parseInt(s.substring(1));
- switch (dir) {
- case "R":
- pos1.add(dist, 0);
- break;
- case "L":
- pos1.add(-dist, 0);
- break;
- case "U":
- pos1.add(0, dist);
- break;
- case "D":
- pos1.add(0, -dist);
- break;
- }
- line1.add(pos1.copy());
- }
- List<Vector2> intersections = new ArrayList<>();
- Vector2 line2a = new Vector2(0,0);
- for(int i = 0; i < lines.get(1).length; i++) {
- String s = lines.get(1)[i];
- String dir = s.substring(0,1);
- int dist = Integer.parseInt(s.substring(1));
- Vector2 line2b = line2a.copy();
- switch (dir) {
- case "R":
- line2b.add(dist, 0);
- break;
- case "L":
- line2b.add(-dist, 0);
- break;
- case "U":
- line2b.add(0, dist);
- break;
- case "D":
- line2b.add(0, -dist);
- break;
- }
- for(int j = 0; j < line1.size() -1; j++) {
- Vector2 line1a = line1.get(j);
- Vector2 line1b = line1.get(j+1);
- boolean intersectionFound = false;
- if(dir.matches("[UD]") && line1a.getY() == line1b.getY()) { //horizontal
- // System.out.println("LINE 1");
- // line1a.show();
- // line1b.show();
- // System.out.println("LINE2");
- // line2a.show();
- // line2b.show();
- int y1 = line1a.getY(), x2 = line2a.getX();
- int x1min = Math.min(line1a.getX(), line1b.getX());
- int x1max = Math.max(line1a.getX(), line1b.getX());
- int y2min = Math.min(line2a.getY(), line2b.getY());
- int y2max = Math.max(line2a.getY(), line2b.getY());
- if(y1 > y2min && y1 < y2max && x1min < x2 && x1max > x2) {
- intersectionFound = true;
- Vector2 intersection = new Vector2(line2a.getX(), line1a.getY());
- intersections.add(intersection);
- }
- } else if(dir.matches("[LR]") && line1a.getX() == line1b.getX()){ //vertical
- int x1 = line1a.getX(), y2 = line2a.getY();
- int y1min = Math.min(line1a.getY(), line1b.getY());
- int y1max = Math.max(line1a.getY(), line1b.getY());
- int x2min = Math.min(line2a.getX(), line2b.getX());
- int x2max = Math.max(line2a.getX(), line2b.getX());
- if(x1 > x2min && x1 < x2max && y1min < y2 && y1max > y2) {
- intersectionFound = true;
- Vector2 intersection = new Vector2(line1a.getX(), line2a.getY());
- intersections.add(intersection);
- }
- }
- }
- line2a = line2b;
- }
- int min = Integer.MAX_VALUE;
- System.out.println("intersections found:");
- for(Vector2 v : intersections) {
- v.show();
- min = Math.min(min, v.manhattanLength());
- }
- System.out.println("shortest distance from intersection is:");
- System.out.println(min);
- // for(Vector2 v : line1)
- // System.out.println(v.getX() + ", " + v.getY());
- }
Advertisement
Add Comment
Please, Sign In to add comment