Advertisement
Guest User

Day06

a guest
Dec 6th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. import org.junit.Test;
  2.  
  3. import java.awt.*;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Collection;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.stream.Collectors;
  11.  
  12. import static org.hamcrest.MatcherAssert.assertThat;
  13. import static org.hamcrest.core.Is.is;
  14.  
  15. public class Day06 extends AdventOfCode {
  16.  
  17.     @Test
  18.     @Override
  19.     public void runTask1 () {
  20.         assertThat(solveTask1(getInputLines()), is(4166));
  21.     }
  22.  
  23.     @Test
  24.     @Override
  25.     public void runTask2 () {
  26.         assertThat(solveTask2(getInputLines(), 10000), is(42250));
  27.     }
  28.  
  29.     @Test
  30.     public void runExample1 () {
  31.         assertThat(solveTask1(Arrays.asList("1, 1",
  32.                                       "1, 6",
  33.                                       "8, 3",
  34.                                       "3, 4",
  35.                                       "5, 5",
  36.                                       "8, 9")), is(17));
  37.     }
  38.  
  39.     @Test
  40.     public void runTask2Example1 () {
  41.         assertThat(solveTask2(Arrays.asList("1, 1",
  42.                 "1, 6",
  43.                 "8, 3",
  44.                 "3, 4",
  45.                 "5, 5",
  46.                 "8, 9"), 32), is(16));
  47.     }
  48.  
  49.     private int solveTask1 (List<String> input) {
  50.         List<Point> coords = input.stream().map(this::parse).collect(Collectors.toList());
  51.  
  52.         translateAll(coords, 500);
  53.  
  54.         Map<Point, Integer> byCoord900= runWithGridSize(coords, 1000, 1000);
  55.  
  56.         translateAll(coords, 600);
  57.         Map<Point, Integer> byCoord1500= runWithGridSize(coords, 2000, 2000);
  58.         translateAll(byCoord1500.keySet(), -600);
  59.  
  60.         // calc delta
  61.         Map<Point, Integer> delta = new HashMap<>();
  62.         for (Map.Entry<Point, Integer> entry : byCoord900.entrySet()) {
  63.             Point p = entry.getKey();
  64.             Integer sizeA = entry.getValue();
  65.  
  66.             // Point equality does not seem to work after translating them :(
  67.             Map.Entry<Point, Integer> entry1500 = byCoord1500.entrySet().stream()
  68.                                   .filter(p2 -> p2.getKey().x == p.x && p2.getKey().y == p.y).findFirst().orElse(null);
  69.  
  70.             if (entry1500  != null && entry1500.getValue().equals(sizeA)) {
  71.                 delta.put(p, entry.getValue());
  72.             }
  73.         }
  74.  
  75.         int max= delta.values().stream().mapToInt(n -> n).max().orElse(-1);
  76.         return max;
  77.     }
  78.  
  79.     private void translateAll (Collection<Point> coords, int delta) {
  80.         coords.forEach(p -> p.translate(delta, delta));
  81.     }
  82.  
  83.     private Map<Point, Integer> runWithGridSize (List<Point> coords, int height, int width) {
  84.         Map<Point, Integer> byCoord= new HashMap<>();
  85.         for (int i = 0; i < height; i++) {
  86.             for (int j = 0; j < width; j++) {
  87.                 Point here = new Point(i, j);
  88.                 Point closest = closest(here, coords);
  89.                 if (closest != null) {
  90.                     Integer value = byCoord.getOrDefault(closest, 0);
  91.                     byCoord.put(closest, value + 1);
  92.                 }
  93.             }
  94.         }
  95. //      print(grid, coords);
  96.         return byCoord;
  97.     }
  98.  
  99.     private void print (Point[][] grid, List<Point> coords) {
  100.         for (int i = 0; i < grid.length; i++) {
  101.             Point[] line = grid[i];
  102.             for (int j = 0; j < line.length; j++) {
  103.                 Point here = line[j];
  104.                 int cIdx= coords.indexOf(here);
  105.  
  106.                 char c = (char) ('a' + cIdx);
  107.                 if (cIdx < 0) {
  108.                     c= '.';
  109.                 }
  110.                 System.out.print(c);
  111.             }
  112.             System.out.println();
  113.         }
  114.     }
  115.  
  116.     private Point closest (Point here, List<Point> coords) {
  117.         int minDist= Integer.MAX_VALUE;
  118.         List<Point> minPoint= new ArrayList<>();
  119.         for (Point c : coords) {
  120.             int delta= Math.abs(here.x - c.x) + Math.abs(here.y - c.y);
  121.             if (delta == minDist) {
  122.                 minPoint.add(c);
  123.             } else  if (delta < minDist) {
  124.                 minDist= delta;
  125.                 minPoint= new ArrayList<>();
  126.                 minPoint.add(c);
  127.             }
  128.         }
  129.         return minPoint.size() > 1 ? null : minPoint.get(0);
  130.     }
  131.  
  132.     private Point parse (String line) {
  133.         String[] tokens= line.split(", ");
  134.         return new Point(Integer.parseInt(tokens[1]), Integer.parseInt(tokens[0]));
  135.     }
  136.  
  137.     private int solveTask2 (List<String> input, int limit) {
  138.         List<Point> coords = input.stream().map(this::parse).collect(Collectors.toList());
  139.  
  140.         translateAll(coords, 500);
  141.         return runTwo(coords, 1000, 1000, limit);
  142.     }
  143.  
  144.     private int runTwo (List<Point> coords, int height, int width, int limit) {
  145.         int regionSize = 0;
  146.         for (int i = 0; i < height; i++) {
  147.             for (int j = 0; j < width; j++) {
  148.                 Point here = new Point(i, j);
  149.                 int dist= distanceTo(here, coords, limit);
  150.                 if (dist > 0) {
  151.                     regionSize+= 1;
  152.                 }
  153.             }
  154.         }
  155.         return regionSize;
  156.     }
  157.  
  158.     private int distanceTo (Point here, List<Point> coords, int limit) {
  159.         int dist= 0;
  160.         for (Point c : coords) {
  161.             dist += Math.abs(here.x - c.x) + Math.abs(here.y - c.y);
  162.             if (dist >= limit) {
  163.                 return -1;
  164.             }
  165.         }
  166.         return dist;
  167.     }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement