-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasscodeDerivation.java
More file actions
64 lines (53 loc) · 2.27 KB
/
PasscodeDerivation.java
File metadata and controls
64 lines (53 loc) · 2.27 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
package dev;
import java.io.*;
import java.util.*;
public class PasscodeDerivation {
public static void main(String[] args) throws IOException {
// Read the file and store each login attempt
List<String> attempts = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("keylog.txt"))) {
String line;
while ((line = br.readLine()) != null) {
attempts.add(line.trim());
}
}
// Create a graph to store the ordering constraints
Map<Character, Set<Character>> graph = new HashMap<>();
Map<Character, Integer> inDegree = new HashMap<>();
// Initialize the graph and in-degrees for each digit
for (String attempt : attempts) {
char first = attempt.charAt(0);
char second = attempt.charAt(1);
char third = attempt.charAt(2);
graph.putIfAbsent(first, new HashSet<>());
graph.putIfAbsent(second, new HashSet<>());
graph.putIfAbsent(third, new HashSet<>());
inDegree.putIfAbsent(first, 0);
inDegree.putIfAbsent(second, 0);
inDegree.putIfAbsent(third, 0);
// Build the graph and update in-degrees based on order constraints
if (graph.get(first).add(second)) inDegree.put(second, inDegree.get(second) + 1);
if (graph.get(second).add(third)) inDegree.put(third, inDegree.get(third) + 1);
}
// Topological sort to determine the shortest passcode
Queue<Character> queue = new LinkedList<>();
for (Character key : inDegree.keySet()) {
if (inDegree.get(key) == 0) {
queue.add(key);
}
}
StringBuilder passcode = new StringBuilder();
while (!queue.isEmpty()) {
Character node = queue.poll();
passcode.append(node);
for (Character neighbor : graph.get(node)) {
inDegree.put(neighbor, inDegree.get(neighbor) - 1);
if (inDegree.get(neighbor) == 0) {
queue.add(neighbor);
}
}
}
// Output the shortest possible passcode
System.out.println("The shortest possible passcode is: " + passcode.toString());
}
}