-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuzzle6.java
More file actions
59 lines (54 loc) · 1.84 KB
/
Copy pathPuzzle6.java
File metadata and controls
59 lines (54 loc) · 1.84 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
package advent2022;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.CharStreams;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
/**
* @author Éamonn McManus
*/
public class Puzzle6 {
private static final String SAMPLE =
"""
mjqjpqmgbljsphdztnvjfqwrcgsmlb
bvwbjplbgvbhsrlpgdmjqwftvncz
nppdvjthqldpwncqszvftbrmjlhg
nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg
zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw
""";
private static final Map<String, Callable<Reader>> INPUT_PRODUCERS =
ImmutableMap.of(
"sample", () -> new StringReader(SAMPLE),
"problem", () -> new InputStreamReader(Puzzle6.class.getResourceAsStream("puzzle6.txt")));
public static void main(String[] args) throws Exception {
for (var entry : INPUT_PRODUCERS.entrySet()) {
String name = entry.getKey();
try (Reader r = entry.getValue().call()) {
List<String> lines = CharStreams.readLines(r);
List<Integer> packetStarts = lines.stream().map(line -> findStart(line, 4)).toList();
System.out.println("Packet starts for " + name + ": " + packetStarts);
List<Integer> messageStarts = lines.stream().map(line -> findStart(line, 14)).toList();
System.out.println("Message starts for " + name + ": " + messageStarts);
}
}
}
private static int findStart(String s, int len) {
Deque<Character> recent = new ArrayDeque<>();
for (int i = 0; i < s.length(); i++) {
if (recent.size() == len) {
recent.removeFirst();
}
recent.addLast(s.charAt(i));
if (new HashSet<>(recent).size() == len) {
return i + 1;
}
}
return -1;
}
}