Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. package com.eulersboiler.advent2018.day09;
  2.  
  3. import java.awt.Point;
  4. import java.io.IOException;
  5. import java.nio.file.Files;
  6. import java.nio.file.Paths;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. public class Day17 {
  11.     public static void main(String[] args) throws IOException {
  12.         List<String> lines = Files.readAllLines(Paths.get("day17.txt"));
  13.         char[][] map = new char[2000][2000];
  14.         for (int x = 0; x < map.length; x++) {
  15.             for (int y = 0; y < map[x].length; y++) {
  16.                 map[x][y] = '.';
  17.             }
  18.         }
  19.         map[500][0] = '|';
  20.         int minY = Integer.MAX_VALUE;
  21.         int maxY = 0;
  22.         for (int i = 0; i < lines.size(); i++) {
  23.             String c = lines.get(i).replaceAll(", ", " ").replaceAll("=", " ").replaceAll("\\.\\.", " ");
  24.             String[] cur = c.split(" ");
  25.             if (cur[0].charAt(0) == 'x') {
  26.                 int x = Integer.parseInt(cur[1]);
  27.                 int y1 = Integer.parseInt(cur[3]);
  28.                 int y2 = Integer.parseInt(cur[4]);
  29.                 for (int y = y1; y <= y2; y++) {
  30.                     map[x][y] = '#';
  31.                 }
  32.                 if (y2 > maxY) {
  33.                     maxY = y2;
  34.                 }
  35.                 if (y1 < minY) {
  36.                     minY = y1;
  37.                 }
  38.             } else {
  39.                 int y = Integer.parseInt(cur[1]);
  40.                 int x1 = Integer.parseInt(cur[3]);
  41.                 int x2 = Integer.parseInt(cur[4]);
  42.                 for (int x = x1; x <= x2; x++) {
  43.                     map[x][y] = '#';
  44.                 }
  45.                 if (y > maxY) {
  46.                     maxY = y;
  47.                 }
  48.                 if (y < minY) {
  49.                     minY = y;
  50.                 }
  51.             }
  52.         }
  53.  
  54.         ArrayList<Point> ps = new ArrayList<>();
  55.         ps.add(new Point(500, 0));
  56.  
  57.         while (ps.size() > 0) {
  58.             ArrayList<Point> newP = new ArrayList<>();
  59.             if (ps.size() >= 3) {
  60.                 System.out.print("");
  61.             }
  62.             ArrayList<Point> temp = new ArrayList<>();
  63.             while (ps.size() > 0) {
  64.                 int mY = Integer.MAX_VALUE;
  65.                 int ID = 0;
  66.                 for (int i = 0; i < ps.size(); i++) {
  67.                     if (ps.get(i).getY() < mY) {
  68.                         mY = (int) ps.get(i).getY();
  69.                         ID = i;
  70.                     }
  71.                 }
  72.                 temp.add(ps.remove(ID));
  73.             }
  74.             ps = temp;
  75.             for (Point p : ps) {
  76.                 int x = (int) p.getX();
  77.                 int y = (int) p.getY();
  78.                 if (map[x][y + 1] == '.') {
  79.                     if ((map[x][y + 2] != '|' && map[x][y + 2] != '~')
  80.                             || !(!scanLeft(map, x, y + 2) || !scanRight(map, x, y + 2))) {
  81.  
  82.                         map[x][y + 1] = '|';
  83.                         p.move(x, y + 1);
  84.                         newP.add(p);
  85.                     } else {
  86.                         map[x][y + 1] = '|';
  87.                     }
  88.  
  89.                 } else {
  90.                     map[x][y] = '~';
  91.                     boolean flag = true;
  92.                     boolean f = false;
  93.                     int ix = x;
  94.                     int iy = y;
  95.                     while (map[x - 1][y] != '#' && map[x][y + 1] != '.' && flag) {
  96.                         x = x - 1;
  97.                         map[x][y] = '~';
  98.                         if (map[x][y + 1] == '.' && (map[x + 1][y + 1] != '|' && map[x + 1][y + 1] != '~')) {
  99.                             flag = false;
  100.                             newP.add(new Point(x, y));
  101.                             f = true;
  102.                         }
  103.                         if (map[x][y + 1] == '.' && (map[x + 1][y + 1] == '|' || map[x + 1][y + 1] == '~')) {
  104.                             map[x][y] = '.';
  105.                         }
  106.                     }
  107.  
  108.                     x = ix;
  109.                     y = iy;
  110.  
  111.                     flag = true;
  112.                     while (map[x + 1][y] != '#' && map[x][y + 1] != '.' && flag) {
  113.  
  114.                         x = x + 1;
  115.                         map[x][y] = '~';
  116.                         if (map[x][y + 1] == '.' && (map[x - 1][y + 1] != '|' && map[x - 1][y + 1] != '~')) {
  117.                             flag = false;
  118.                             newP.add(new Point(x, y));
  119.                             f = true;
  120.                         }
  121.                         if (map[x][y + 1] == '.' && (map[x - 1][y + 1] == '|' || map[x - 1][y + 1] == '~')) {
  122.                             map[x][y] = '.';
  123.                         }
  124.                     }
  125.                     if (!f && scanLeft(map, ix, iy) && scanRight(map, ix, iy)) {
  126.  
  127.                         newP.add(new Point(ix, iy - 1));
  128.                     }
  129.                 }
  130.             }
  131.             ps = newP;
  132.             trim(ps, maxY);
  133.         }
  134.         int sum = 0;
  135.         for (int x = 0; x < map.length; x++) {
  136.             for (int y = minY; y <= maxY; y++) {
  137.                 if (map[x][y] == '|' || map[x][y] == '~')
  138.                     sum++;
  139.             }
  140.         }
  141.         System.out.println(sum);
  142.  
  143.         for (int y = 0; y < map[0].length; y++) {
  144.             for (int x = 1; x < map.length; x++) {
  145.                 if (map[x - 1][y] == '.' && map[x][y] == '~') {
  146.                     for (int x2 = x; map[x2][y] == '~'; x2++) {
  147.                         x = x2;
  148.                         map[x2][y] = '|';
  149.                     }
  150.                 }
  151.             }
  152.         }
  153.         for (int y = 0; y < map[0].length; y++) {
  154.             for (int x = map.length - 2; x > 0; x--) {
  155.                 if (map[x + 1][y] == '.' && map[x][y] == '~') {
  156.                     for (int x2 = x; map[x2][y] == '~'; x2--) {
  157.                         x = x2;
  158.                         map[x2][y] = '|';
  159.                     }
  160.                 }
  161.             }
  162.         }
  163.  
  164.         sum = 0;
  165.         for (int x = 0; x < map.length; x++) {
  166.             for (int y = minY; y <= maxY; y++) {
  167.                 if (map[x][y] == '~')
  168.                     sum++;
  169.             }
  170.         }
  171.         System.out.println(sum);
  172.     }
  173.  
  174.     private static void trim(ArrayList<Point> ps, int maxY) {
  175.         for (int i = 0; i < ps.size(); i++) {
  176.             if (ps.get(i).getY() > maxY) {
  177.                 ps.remove(i);
  178.                 i--;
  179.             }
  180.         }
  181.     }
  182.  
  183.     private static boolean scanLeft(char[][] map, int x, int y) {
  184.         while (map[x - 1][y] != '#' && map[x - 1][y + 1] != '.') {
  185.             x = x - 1;
  186.         }
  187.         if (map[x - 1][y + 1] == '.') {
  188.             return false;
  189.         }
  190.         return true;
  191.     }
  192.  
  193.     private static boolean scanRight(char[][] map, int x, int y) {
  194.         while (map[x + 1][y] != '#' && map[x + 1][y + 1] != '.') {
  195.             x = x + 1;
  196.         }
  197.         if (map[x + 1][y + 1] == '.') {
  198.             return false;
  199.         }
  200.         return true;
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement