-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay6.java
More file actions
52 lines (44 loc) · 1.15 KB
/
Day6.java
File metadata and controls
52 lines (44 loc) · 1.15 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
import java.util.ArrayList;
import java.util.LinkedList;
public class Day6 {
public static void main(String[] args) {
new Day6();
}
public Day6() {
ArrayList<String> input = ReadInput.read("res/input6.txt");
partOne(input);
partTwo(input);
}
private void partOne(ArrayList<String> input) {
System.out.println(getMarker(input.get(0), 4));
}
private void partTwo(ArrayList<String> input) {
System.out.println(getMarker(input.get(0), 14));
}
private int getMarker(String input, int distinctCharacters) {
LinkedList<Character> queue = new LinkedList<Character>();
for(int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
queue.add(c);
if(queue.size() < distinctCharacters)
continue;
if(queueHasUniqueValues(queue)) {
return i + 1;
}
queue.poll();
}
return -1;
}
private boolean queueHasUniqueValues(LinkedList<Character> queue) {
char[] chars = new char[queue.size()];
for(int i = 0; i < chars.length; i++)
chars[i] = queue.get(i);
for(int i = 0; i < chars.length; i++) {
for(int j = i + 1; j < chars.length; j++) {
if(chars[i] == chars[j])
return false;
}
}
return true;
}
}