Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advent2018;
- import util.AdventOfCode;
- import util.Direction;
- import util.Node;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- public class Day6 extends AdventOfCode {
- List<Node> nodes;
- Map<Integer, Integer> counts;
- static final int GRID_SIZE = 1000;
- public Day6(List<String> input) {
- super(input);
- title = "Chronal Coordinates";
- part1Description = "Largest non-infinite region: ";
- part2Description = "Largest region summing to < 10000: ";
- }
- @Override
- public Object part1() {
- counts = new HashMap<>();
- Set<Integer> inf = new HashSet<>();
- for (int i = 0; i < GRID_SIZE; i++) {
- for (int j = 0; j < GRID_SIZE; j++) {
- int dist = getClosest(j, i);
- counts.merge(dist, 1, (x, y) -> x + y);
- // add infinite to set
- if (i == 0 || i == GRID_SIZE - 1 || j == 0 || j == GRID_SIZE - 1) {
- inf.add(dist);
- }
- }
- }
- return IntStream.range(0, nodes.size()).boxed()
- .filter(x -> !inf.contains(x))
- .map(counts::get)
- .mapToInt(x -> x)
- .max().getAsInt();
- }
- private int getClosest(int x, int y) {
- Node node = new Node(x, y);
- int min = 10000;
- int minIndex = 0;
- for (int i = 0; i < nodes.size(); i++) {
- int dist = Direction.manhattanDistance(node, nodes.get(i));
- if (dist < min) {
- min = dist;
- minIndex = i;
- } else {
- if (dist == min) {
- min = dist;
- minIndex = -1;
- }
- }
- }
- return minIndex;
- }
- @Override
- public Object part2() {
- int count = 0;
- for (int i = 0; i < GRID_SIZE; i++) {
- for (int j = 0; j < GRID_SIZE; j++) {
- Node current = new Node(j, i);
- int sum = 0;
- for (Node each : nodes) {
- sum += Direction.manhattanDistance(current, each);
- }
- if (sum < 10000) count++;
- }
- }
- return count;
- }
- @Override
- public void parse() {
- nodes = input.stream()
- .map(x -> new Node(Integer.parseInt(x.split(", ")[0]),
- Integer.parseInt(x.split(", ")[1])))
- .collect(Collectors.toList());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement