Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2018
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.72 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 Day22 extends AdventOfCode {
  10.  
  11.     int depth;
  12.     int targetX;
  13.     int targetY;
  14.  
  15.     enum Type {
  16.         ROCK(Tool.NONE), WET(Tool.TORCH), NARROW(Tool.CLIMB);
  17.  
  18.         Tool invalid;
  19.         Type(Tool invalid) {
  20.             this.invalid = invalid;
  21.         }
  22.  
  23.         static Type getByNum(int n) {
  24.             return Type.values()[n];
  25.         }
  26.     }
  27.  
  28.     enum Tool {
  29.         NONE(Type.ROCK), TORCH(Type.WET), CLIMB(Type.NARROW);
  30.  
  31.         Type invalid;
  32.  
  33.         Tool(Type invalid) {
  34.             this.invalid = invalid;
  35.         }
  36.  
  37.         static Tool getByNum(int n) {
  38.             return Tool.values()[n];
  39.         }
  40.     }
  41.  
  42.     class Cave {
  43.         Node pos;
  44.         int minutes;
  45.         Tool tool;
  46.         Type type;
  47.  
  48.         public Cave(Node pos, int minutes, Tool tool, Type type) {
  49.             this.pos = pos;
  50.             this.minutes = minutes;
  51.             this.tool = tool;
  52.             this.type = type;
  53.         }
  54.  
  55.         @Override
  56.         public boolean equals(Object o) {
  57.             if (this == o) return true;
  58.             if (o == null || getClass() != o.getClass()) return false;
  59.  
  60.             Cave cave = (Cave) o;
  61.  
  62.             if (pos != null ? !pos.equals(cave.pos) : cave.pos != null) return false;
  63.             return tool == cave.tool;
  64.         }
  65.  
  66.         @Override
  67.         public int hashCode() {
  68.             int result = pos != null ? pos.hashCode() : 0;
  69.             result = 31 * result + (tool != null ? tool.hashCode() : 0);
  70.             return result;
  71.         }
  72.  
  73.         int getMinutes() { return minutes;}
  74.     }
  75.  
  76.  
  77.  
  78.     int[][] grid = new int[1000][1000];
  79.     int[][] erosion = new int[1000][1000];
  80.     int[][] geolog = new int[1000][1000];
  81.  
  82.  
  83.     public Day22(List<String> input) {
  84.         super(input);
  85.     }
  86.  
  87.     int erosion(int x, int y) {
  88.         return (geolog[y][x] + depth) % 20183;
  89.     }
  90.  
  91.     int type(int x, int y) {
  92.         return erosion[y][x] % 3;
  93.     }
  94.  
  95.     int geologic(int x, int y) {
  96.         if (y == 0) {
  97.             if (x == 0) return 0;
  98.             return x * 16807;
  99.         }
  100.         if (x == 0) {
  101.             return y * 48271;
  102.         }
  103.         if (x == targetX && y == targetY) {
  104.             return 0;
  105.         }
  106.         else {
  107.             return erosion[y - 1][x] * erosion[y][x - 1];
  108.         }
  109.     }
  110.  
  111.     @Override
  112.     public Object part1() {
  113.         int sum = 0;
  114.         for (int y = 0; y < 1000; y++) {
  115.             for (int x = 0; x < 1000; x++) {
  116.                 geolog[y][x] = geologic(x, y);
  117.                 erosion[y][x] = erosion(x, y);
  118.                 grid[y][x] = type(x, y);
  119.  
  120.                 if (y <= targetY && x <= targetX) {
  121.  
  122.                     sum += grid[y][x];
  123.                 }
  124.             }
  125.         }
  126.         return sum;
  127.     }
  128.  
  129.     @Override
  130.     public Object part2() {
  131.         PriorityQueue<Cave> queue = new PriorityQueue<>(Comparator.comparing(Cave::getMinutes));
  132.         queue.add(new Cave(new Node(0, 0), 1, Tool.TORCH, Type.ROCK));
  133.         Map<Cave, Integer> minutes = new HashMap<>();
  134.         Cave target = new Cave(new Node(targetX, targetY), 0, Tool.TORCH, Type.ROCK);
  135.  
  136.         while (!queue.isEmpty()) {
  137.             Cave current = queue.poll();
  138.             if (minutes.containsKey(current) && minutes.get(current) <= current.getMinutes()) {
  139.                 continue;
  140.             }
  141.             minutes.put(current, current.minutes);
  142.             if (current.equals(target)) {
  143.                 return current.minutes;
  144.             }
  145.            
  146.             // find other valid tool
  147.             for (Tool tool : Tool.values()) {
  148.                 if (tool != current.tool && tool != current.type.invalid) {
  149.                     queue.add(new Cave(current.pos, current.minutes + 7, tool, current.type));
  150.                 }
  151.             }
  152.            
  153.             // check other directions
  154.             for (Direction dir : Direction.values()) {
  155.                 int nx = current.pos.getX() + dir.getDx();
  156.                 int ny = current.pos.getY() + dir.getDy();
  157.                 if (Direction.rangeCheck(nx, ny, 0, 0, 1000, 1000)
  158.                     && Type.getByNum(grid[ny][nx]) != current.tool.invalid) {
  159.                     queue.add(new Cave(new Node(nx, ny), current.minutes + 1,
  160.                             current.tool, Type.getByNum(grid[ny][nx])));
  161.                 }
  162.             }
  163.         }
  164.         return "";
  165.     }
  166.  
  167.     @Override
  168.     public void parse() {
  169.         depth = Integer.parseInt(input.get(0).split(" ")[1]);
  170.         String[] t = input.get(1).split(" ")[1].split(",");
  171.         targetX = Integer.parseInt(t[0]);
  172.         targetY = Integer.parseInt(t[1]);
  173.     }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement