Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.33 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.  
  9. public class Day13 extends AdventOfCode {
  10.  
  11.     static final int GRID_SIZE = 150;
  12.     static final String CART_DIR = "^>v<";
  13.  
  14.     String[][] grid;
  15.     List<Cart> carts;
  16.     String first = "";
  17.     String lastCart = "";
  18.  
  19.  
  20.     enum Turn {
  21.         LEFT, STRAIGHT, RIGHT
  22.     }
  23.  
  24.     class Cart {
  25.         int num;
  26.         Node position;
  27.         Direction dir;
  28.         Turn last = Turn.RIGHT;
  29.         boolean collided;
  30.         int getY() { return position.getY(); }
  31.         int getX() { return position.getX(); }
  32.  
  33.         void move() {
  34.  
  35.             position = dir.move(position);
  36.             for (Cart each :carts) {
  37.                 if (!each.equals(this)) {
  38.                     if (!each.collided) {
  39.                         if (each.position.equals(this.position)) {
  40.                             if (first.length() == 0) first = getX() + "," + getY();
  41.                             System.out.println("Collision at " + getX() + "," + getY());
  42.                             System.out.println("Between " + this.num + " and " + each.num);
  43.                             this.collided = true;
  44.                             each.collided = true;
  45.                             return;
  46.                         }
  47.                     }
  48.  
  49.                 }
  50.             }
  51.  
  52.             String next = grid[position.getY()][position.getX()];
  53.             switch (next) {
  54.                 case "/":
  55.                     switch (dir) {
  56.                         case NORTH:
  57.                         case SOUTH:
  58.                             dir = dir.getRight();
  59.                             break;
  60.                         case EAST:
  61.                         case WEST:
  62.                             dir = dir.getLeft();
  63.                             break;
  64.                     }
  65.                     break;
  66.                 case "\\":
  67.                     switch (dir) {
  68.                         case NORTH:
  69.                         case SOUTH:
  70.                             dir = dir.getLeft();
  71.                             break;
  72.                         case EAST:
  73.                         case WEST:
  74.                             dir = dir.getRight();
  75.                             break;
  76.                     }
  77.                     break;
  78.                 case "+":
  79.                     switch (last) {
  80.                         case RIGHT:
  81.                             last = Turn.LEFT;
  82.                             dir = dir.getLeft();
  83.                             break;
  84.                         case STRAIGHT:
  85.                             last = Turn.RIGHT;
  86.                             dir = dir.getRight();
  87.                             break;
  88.                         case LEFT:
  89.                             last = Turn.STRAIGHT;
  90.                             break;
  91.                     }
  92.             }
  93.         }
  94.     }
  95.  
  96.     public Day13(List<String> input) {
  97.         super(input);
  98.     }
  99.  
  100.  
  101.     @Override
  102.     public Object part1() {
  103.  
  104.         int count = carts.size();
  105.  
  106.         while (count > 1) {
  107.             carts.sort(Comparator.comparing(Cart::getY).thenComparing(Cart::getX));
  108.             count = (int) carts.stream().filter(x -> !x.collided).count();
  109.             for (Cart each : carts) {
  110.                 if (!each.collided) each.move();
  111.             }
  112.         }
  113.         Cart last = carts.stream().filter(x -> !x.collided).findFirst().get();
  114.         lastCart = last.position.getX() + "," + last.position.getY();
  115.  
  116.         return first;
  117.     }
  118.  
  119.     @Override
  120.     public Object part2() {
  121.         return lastCart;
  122.     }
  123.  
  124.     @Override
  125.     public void parse() {
  126.  
  127.         carts = new ArrayList<>();
  128.         grid = new String[GRID_SIZE][GRID_SIZE];
  129.         int count = 0;
  130.         for (int i = 0; i < input.size(); i++) {
  131.             String line = input.get(i);
  132.             for (int j = 0; j < line.length(); j++) {
  133.                 String cell = Character.toString(line.charAt(j));
  134.                 grid [i][j] = cell;
  135.                 if (CART_DIR.contains(cell)) {
  136.                     Cart c = new Cart();
  137.                     c.num = count++;
  138.                     c.position = new Node(j, i);
  139.                     c.dir = Direction.getByNum(CART_DIR.indexOf(cell));
  140.                     carts.add(c);
  141.                 }
  142.             }
  143.         }
  144.  
  145.     }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement