-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay06.java
More file actions
38 lines (36 loc) · 1.15 KB
/
Day06.java
File metadata and controls
38 lines (36 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
import java.util.List;
public class Day06 {
public static void Run(List<String> input)
{
for(int i=0,n=input.size();i<n;i++){
String line =input.get(i);
System.out.println("Line "
+(i+1)
+" 06.1: "
+findMarker(line,4)
+" 06.2: "
+findMarker(line,14));
}
}
public static int findMarker(String input, int keyLength)
{
String keyBuffer ="";
int syncPosition,length;
for (syncPosition=0,length=input.length();syncPosition<length;syncPosition++)
{
char nextChar = input.charAt(syncPosition);
int matchPos = keyBuffer.indexOf(nextChar);
if (matchPos > -1)
{
// remove up to and including match.
// can only be one match because key contains no duplicates
keyBuffer = keyBuffer.substring(matchPos+1);
}
keyBuffer=keyBuffer+nextChar;
if (keyBuffer.length() == keyLength)
break;
}
//return 1-based position
return syncPosition+1;
}
}