Advertisement
Guest User

Untitled

a guest
Dec 25th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. package Advent2018;
  2.  
  3. import util.AdventOfCode;
  4. import util.QuickUnionUF;
  5. import util.UnionFind;
  6.  
  7. import java.util.*;
  8. import java.util.stream.Collectors;
  9.  
  10. public class Day25 extends AdventOfCode {
  11.  
  12.     List<Point4d> points;
  13.  
  14.  
  15.     class Point4d {
  16.         int x;
  17.         int y;
  18.         int z;
  19.         int u;
  20.  
  21.         Point4d(int x, int y, int z, int u) {
  22.             this.x = x;
  23.             this.y = y;
  24.             this.z = z;
  25.             this.u = u;
  26.         }
  27.        
  28.         Point4d(List<Integer> in) {
  29.             this(in.get(0), in.get(1), in.get(2), in.get(3));
  30.         }
  31.  
  32.         int dist(Point4d other) {
  33.             return Math.abs(this.x - other.x) + Math.abs(this.y - other.y)
  34.                     + Math.abs(this.z - other.z) + Math.abs(this.u - other.u);
  35.         }
  36.     }
  37.  
  38.     public Day25(List<String> input) {
  39.         super(input);
  40.     }
  41.  
  42.     @Override
  43.     public Object part1() {
  44.  
  45.         QuickUnionUF uf = new QuickUnionUF(points.size());
  46.         for (int i = 0; i < points.size() - 1; i++) {
  47.             for (int j = i + 1; j < points.size(); j++) {
  48.                 if (points.get(i).dist(points.get(j)) <= 3) {
  49.                     if (!uf.connected(i, j))  {
  50.                         uf.union(i, j);
  51.                     }
  52.                 }
  53.  
  54.             }
  55.         }
  56.         return uf.count();
  57.     }
  58.  
  59.     @Override
  60.     public Object part2() {
  61.         return "Merry Christmas!!!";
  62.     }
  63.  
  64.     @Override
  65.     public void parse() {
  66.         points = input.stream()
  67.                 .map(x -> Arrays.stream(x.split(","))
  68.                         .mapToInt(Integer::parseInt)
  69.                         .boxed()
  70.                         .collect(Collectors.toList()))
  71.                 .map(Point4d::new)
  72.                 .collect(Collectors.toList());
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement