Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. package Advent2018;
  2.  
  3. import util.AdventOfCode;
  4. import util.Direction;
  5. import util.Node;
  6.  
  7. import java.util.*;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.IntStream;
  10.  
  11. public class Day6 extends AdventOfCode {
  12.  
  13.     List<Node> nodes;
  14.     Map<Integer, Integer> counts;
  15.     static final int GRID_SIZE = 1000;
  16.  
  17.     public Day6(List<String> input) {
  18.         super(input);
  19.         title = "Chronal Coordinates";
  20.         part1Description = "Largest non-infinite region: ";
  21.         part2Description = "Largest region summing to < 10000: ";
  22.     }
  23.  
  24.     @Override
  25.     public Object part1() {
  26.         counts = new HashMap<>();
  27.         Set<Integer> inf = new HashSet<>();
  28.         for (int i = 0; i < GRID_SIZE; i++) {
  29.             for (int j = 0; j < GRID_SIZE; j++) {
  30.                 int dist = getClosest(j, i);
  31.                 counts.merge(dist, 1, (x, y) -> x + y);
  32.  
  33.                 // add infinite to set
  34.                 if (i == 0 || i == GRID_SIZE - 1 || j == 0 || j == GRID_SIZE - 1) {
  35.                     inf.add(dist);
  36.                 }
  37.             }
  38.         }
  39.  
  40.         return IntStream.range(0, nodes.size()).boxed()
  41.                 .filter(x -> !inf.contains(x))
  42.                 .map(counts::get)
  43.                 .mapToInt(x -> x)
  44.                 .max().getAsInt();
  45.     }
  46.  
  47.     private int getClosest(int x, int y) {
  48.         Node node = new Node(x, y);
  49.         int min = 10000;
  50.         int minIndex = 0;
  51.         for (int i = 0; i < nodes.size(); i++) {
  52.             int dist = Direction.manhattanDistance(node, nodes.get(i));
  53.             if (dist < min) {
  54.                 min = dist;
  55.                 minIndex = i;
  56.             } else {
  57.                 if (dist == min) {
  58.                     min = dist;
  59.                     minIndex = -1;
  60.                 }
  61.             }
  62.         }
  63.         return minIndex;
  64.     }
  65.  
  66.     @Override
  67.     public Object part2() {
  68.         int count = 0;
  69.         for (int i = 0; i < GRID_SIZE; i++) {
  70.             for (int j = 0; j < GRID_SIZE; j++) {
  71.                 Node current = new Node(j, i);
  72.                 int sum = 0;
  73.                 for (Node each : nodes) {
  74.                     sum += Direction.manhattanDistance(current, each);
  75.                 }
  76.                 if (sum < 10000) count++;
  77.             }
  78.         }
  79.         return count;
  80.     }
  81.  
  82.     @Override
  83.     public void parse() {
  84.         nodes = input.stream()
  85.                 .map(x -> new Node(Integer.parseInt(x.split(", ")[0]),
  86.                         Integer.parseInt(x.split(", ")[1])))
  87.                 .collect(Collectors.toList());
  88.  
  89.     }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement