-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDay17.java
More file actions
93 lines (76 loc) · 2.56 KB
/
Day17.java
File metadata and controls
93 lines (76 loc) · 2.56 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
package org.ck.adventofcode.year2016;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import org.ck.adventofcode.util.AOCSolution;
import org.ck.codechallengelib.annotation.Solution;
@Solution(
id = 20161701,
name = "Day 17: Two Steps Forward",
url = "https://adventofcode.com/2016/day/17",
category = "2016")
@Solution(
id = 20161702,
name = "Day 17: Two Steps Forward",
url = "https://adventofcode.com/2016/day/17#part2",
category = "2016")
public class Day17 extends AOCSolution {
private static final Set<Character> OPEN_DOORS = Set.of('b', 'c', 'd', 'e', 'f');
@Override
protected void runPartOne(final Scanner in) {
run(in, false);
}
@Override
protected void runPartTwo(final Scanner in) {
run(in, true);
}
private void run(final Scanner in, final boolean collectAll) {
final String passcode = in.nextLine();
final Queue<State> queue = new PriorityQueue<>(Comparator.comparingInt(s -> s.path().length()));
queue.add(new State(0, 0, ""));
final List<State> paths = new ArrayList<>();
final MessageDigest md = getMd5();
while (!queue.isEmpty()) {
final State state = queue.poll();
if (state.x() == 3 && state.y() == 3) {
if (collectAll) {
paths.add(state);
continue;
} else {
print(state.path());
break;
}
}
md.update((passcode + state.path()).getBytes());
final String hex = HexFormat.of().formatHex(md.digest());
if (state.y > 0 && OPEN_DOORS.contains(hex.charAt(0))) {
queue.add(new State(state.x(), state.y() - 1, state.path() + "U"));
}
if (state.y < 3 && OPEN_DOORS.contains(hex.charAt(1))) {
queue.add(new State(state.x(), state.y() + 1, state.path() + "D"));
}
if (state.x > 0 && OPEN_DOORS.contains(hex.charAt(2))) {
queue.add(new State(state.x() - 1, state.y(), state.path() + "L"));
}
if (state.x < 3 && OPEN_DOORS.contains(hex.charAt(3))) {
queue.add(new State(state.x() + 1, state.y(), state.path() + "R"));
}
}
if (collectAll) {
print(
paths.stream()
.max(Comparator.comparingInt(s -> s.path().length()))
.orElseThrow()
.path()
.length());
}
}
private static MessageDigest getMd5() {
try {
return MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
}
private record State(int x, int y, String path) {}
}