Guest User

Untitled

a guest
Dec 3rd, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. package adventofcode;
  2.  
  3. import adventofcode.utils.Utils;
  4. import java.awt.Point;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.stream.Collectors;
  10.  
  11. public class Day3 {
  12.     private static final List<Map<Point, Integer>> STEPS_TO_POINT = new ArrayList<>();
  13.  
  14.     public static void solve() {
  15.         List<String> input = Utils.getInput(3);
  16.  
  17.         List<Point> wire1 = getAllLocations(0, input.get(0));
  18.         List<Point> wire2 = getAllLocations(1, input.get(1));
  19.  
  20.         List<Point> intersections = wire1.stream()
  21.                 .filter(wire2::contains)
  22.                 .collect(Collectors.toList());
  23.  
  24.         int closest = intersections.stream()
  25.                 .mapToInt(Day3::manhattanDistanceFromOrigin)
  26.                 .min().getAsInt();
  27.         int fastest = intersections.stream()
  28.                 .mapToInt(Day3::combinedStepsToPoint)
  29.                 .min().getAsInt();
  30.  
  31.         System.out.println(closest);
  32.         System.out.println(fastest);
  33.     }
  34.  
  35.     private static List<Point> getAllLocations(int n, String wire) {
  36.         String[] route = wire.split(",");
  37.         List<Point> locations = new ArrayList<>();
  38.         STEPS_TO_POINT.add(new HashMap<>());
  39.  
  40.         int x = 0;
  41.         int y = 0;
  42.         int steps = 0;
  43.  
  44.         for(String section : route) {
  45.             char direction = section.charAt(0);
  46.             int length = Integer.parseInt(section.substring(1));
  47.  
  48.             for(int i = 0 ; i < length ; i++) {
  49.                 switch(direction) {
  50.                     case 'U': y++; break;
  51.                     case 'R': x++; break;
  52.                     case 'D': y--; break;
  53.                     case 'L': x--; break;
  54.                     default: throw new IllegalStateException("HCF");
  55.                 }
  56.  
  57.                 steps++;
  58.                 Point point = new Point(x, y);
  59.                 locations.add(point);
  60.                 if(!STEPS_TO_POINT.get(n).containsKey(point)) {
  61.                     STEPS_TO_POINT.get(n).put(point, steps);
  62.                 }
  63.             }
  64.         }
  65.  
  66.         return locations;
  67.     }
  68.  
  69.     private static int manhattanDistanceFromOrigin(Point point) {
  70.         return Math.abs(point.x) + Math.abs(point.y);
  71.     }
  72.  
  73.     private static int combinedStepsToPoint(Point point) {
  74.         return STEPS_TO_POINT.get(0).get(point) + STEPS_TO_POINT.get(1).get(point);
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment