Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advent2018;
- import graph.DGraph;
- import graph.Graph;
- import graph.SearchType;
- import util.AdventOfCode;
- import util.Direction;
- import util.Node;
- import java.util.*;
- public class Day20 extends AdventOfCode {
- Graph<Node> graph;
- String in;
- Node root = new Node(0, 0);
- long part2;
- static Map<Character, Direction> dirs = new HashMap<>();
- static {
- dirs.put('N', Direction.NORTH);
- dirs.put('E', Direction.EAST);
- dirs.put('S', Direction.SOUTH);
- dirs.put('W', Direction.WEST);
- }
- public Day20(List<String> input) {
- super(input);
- title = "A Regular Map";
- part1Description = "Longest shortest path: ";
- part2Description = "Paths >= 1000 doors: ";
- }
- @Override
- public Object part1() {
- buildGraph(0, root);
- List<List<Node>> paths = new ArrayList<>();
- for (Node node : graph.getVertices()) {
- if (!node.equals(root)) {
- paths.add(graph.search(root, node, SearchType.BFS));
- }
- }
- part2 = paths.stream().mapToInt(List::size).filter(x -> x > 1000).count();
- return paths.stream().mapToInt(List::size).max().getAsInt() - 1;
- }
- @Override
- public Object part2() {
- return part2;
- }
- int buildGraph(int pos, Node n) {
- Node next = n;
- while (pos < in.length() - 1) {
- char c = in.charAt(pos);
- switch (c) {
- case 'N':
- case 'E':
- case 'S':
- case 'W':
- next = dirs.get(c).move(n);
- graph.addEdge(n, next);
- break;
- case '(':
- pos = buildGraph(pos + 1, n);
- while (in.charAt(pos) == '|') {
- pos = buildGraph(pos + 1, n);
- }
- break;
- case '|':
- case ')':
- return pos;
- }
- pos++;
- n = next;
- }
- return pos;
- }
- @Override
- public void parse() {
- graph = new DGraph<>();
- in = input.get(0).substring(1, input.get(0).length() - 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement