Guest User

Untitled

a guest
Dec 3rd, 2019
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. package Advent2019;
  2.  
  3. import util.AdventOfCode;
  4. import util.Direction;
  5. import util.Node;
  6.  
  7. import java.util.*;
  8.  
  9. public class Day3 extends AdventOfCode {
  10.  
  11.     Map<Node, Integer> wire1;
  12.     Map<Node, Integer> intersection;
  13.  
  14.     public Day3(List<String> input) {
  15.         super(input);
  16.         title = "Crossed Wires";
  17.         part1Description = "Closest intersection by distance: ";
  18.         part2Description = "Closest intersection by steps: ";
  19.     }
  20.  
  21.     @Override
  22.     public Object part1() {
  23.         return intersection.entrySet().stream()
  24.                 .map(Map.Entry::getKey)
  25.                 .mapToInt(Direction::manhattanDistance)
  26.                 .min().getAsInt();
  27.     }
  28.  
  29.     @Override
  30.     public Object part2() {
  31.         return intersection.entrySet().stream()
  32.                 .mapToInt(Map.Entry::getValue)
  33.                 .min().getAsInt();
  34.     }
  35.  
  36.     @Override
  37.     public void parse() {
  38.         wire1 = new HashMap<>();
  39.         intersection = new HashMap<>();
  40.  
  41.         Node n = new Node(0, 0);
  42.         int steps = 1;
  43.         for (String each : input.get(0).split(",")) {
  44.             Direction current = Direction.getAlt(each);
  45.             for (int i = 0; i < Integer.parseInt(each.substring(1)); i++) {
  46.                 n = current.move(n);
  47.                 wire1.putIfAbsent(n, steps);
  48.                 steps++;
  49.             }
  50.         }
  51.         n = new Node(0, 0);
  52.         steps = 0;
  53.         for (String each : input.get(1).split(",")) {
  54.             Direction current = Direction.getAlt(each);
  55.             for (int i = 0; i < Integer.parseInt(each.substring(1)); i++) {
  56.                 n = current.move(n);
  57.                 steps++;
  58.                 if (wire1.containsKey(n)) {
  59.                     intersection.putIfAbsent(n, steps + wire1.get(n));
  60.                 }
  61.             }
  62.         }
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment