-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathExplorer.java
More file actions
139 lines (129 loc) · 4.78 KB
/
PathExplorer.java
File metadata and controls
139 lines (129 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Solution to Day 12: Passage Pathing
*
* @author bluebillxp
*/
public class PathExplorer {
private static class Node {
String name;
LinkedList<Node> adjents;
Node(String name) {
this.name = name;
adjents = new LinkedList<>();
}
}
public static void main(String[] args) {
List<String> input = AdventHelper.readInput("input-day12-passage-pathing.txt");
System.out.println("Day 12: Passage Pathing " + input.size() + " commands loaded.");
Map<String, Node> nodeMap = buildNodeMap(input);
System.out.println("Day 12: Passage Pathing --- Part One ---");
System.out.println("How many paths through this cave system are there that visit small caves at most once?");
final int answerOne = solutionPartOne(nodeMap.get("start"));
System.out.println("Answer: " + answerOne);
System.out.println("\nDay 12: Passage Pathing --- Part Two ---");
System.out.println("Given these new rules, how many paths through this cave system are there?");
final int answerTwo = solutionPartTwo(nodeMap.get("start"));
System.out.println("Answer: " + answerTwo);
}
/**
* Calculates how many paths through this cave system are there that visit small caves at most once.
*
* @param startNode Defined starting node from the challenge.
*
* @return answer
*/
private static int solutionPartOne(Node startNode) {
Set<String> totalPaths = new HashSet<>();
exploreSmallCavesOnlyOncePaths(startNode, new LinkedList<>(), totalPaths);
return totalPaths.size();
}
/**
* Calculates how many paths through this cave system are there with new rules.
*
* @param startNode Defined starting node from the challenge.
*
* @return answer
*/
private static int solutionPartTwo(Node startNode) {
Set<String> totalPaths = new HashSet<>();
exploreSmallCavesOnlyTwicePaths(startNode, new LinkedList<>(), totalPaths, true);
return totalPaths.size();
}
private static void exploreSmallCavesOnlyTwicePaths(Node current,
LinkedList<Node> currentPath, Set<String> uniquePaths,
boolean allowSmallCaveTwice) {
// Small caves can only visit once.
boolean smallCave = current.name.toLowerCase() == current.name;
if (smallCave && currentPath.contains(current)) {
if (!allowSmallCaveTwice) {
return;
} else {
allowSmallCaveTwice = false;
}
}
currentPath.add(current);
if (current.name.equals("end")) {
String fullPath = "";
for (Node node : currentPath) {
fullPath += node.name + "-";
}
uniquePaths.add(fullPath);
return;
}
for (Node adjent : current.adjents) {
if (adjent.name.equals("start")) {
continue;
}
exploreSmallCavesOnlyTwicePaths(adjent,
new LinkedList<>(currentPath),
uniquePaths,
allowSmallCaveTwice);
}
}
private static void exploreSmallCavesOnlyOncePaths(Node current,
LinkedList<Node> currentPath, Set<String> uniquePaths) {
// Small caves can only visit once.
if (current.name.toLowerCase() == current.name
&& currentPath.contains(current)) {
return;
}
currentPath.add(current);
if (current.name.equals("end")) {
String fullPath = "";
for (Node node : currentPath) {
fullPath += node.name + "-";
}
uniquePaths.add(fullPath);
return;
}
for (Node adjent : current.adjents) {
exploreSmallCavesOnlyOncePaths(adjent,
new LinkedList<>(currentPath), uniquePaths);
}
}
private static Map<String, Node> buildNodeMap(List<String> input) {
Map<String, Node> nodeMap = new HashMap<>();
for (String line : input) {
String[] nodeNames = line.split("-");
Node leftNode = nodeMap.get(nodeNames[0]);
if (leftNode == null) {
leftNode = new Node(nodeNames[0]);
nodeMap.put(leftNode.name, leftNode);
}
Node rightNode = nodeMap.get(nodeNames[1]);
if (rightNode == null) {
rightNode = new Node(nodeNames[1]);
nodeMap.put(rightNode.name, rightNode);
}
leftNode.adjents.add(rightNode);
rightNode.adjents.add(leftNode);
}
return nodeMap;
}
}