-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day22.java
36 lines (29 loc) · 961 Bytes
/
Day22.java
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
package advent_of_code_2015;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.function.Function;
public class Day22 {
public static <E> E breadthFirstSearch(E start,
Function<E, Boolean> endTest,
Function<E, Iterable<E>> neighbours) {
HashSet<E> s = new HashSet<>();
// HashMap<E, E> parents = new HashMap<>();
ArrayDeque<E> q = new ArrayDeque<>();
s.add(start);
q.add(start);
while (!q.isEmpty()) {
E current = q.remove();
if (endTest.apply(current)) {
return current;
}
for (E n : neighbours.apply(current)) {
if (!s.contains(n)) {
s.add(n);
// parents.put(n, current);
q.add(n);
}
}
}
return null;
}
}