-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day6.java
60 lines (48 loc) · 1.28 KB
/
Day6.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package day6;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Day6 {
public static void main(String[] args) {
try {
String fileName = "input.txt";
Path path = Paths.get(fileName);
Files.readAllBytes(path);
List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
String input = allLines.get(0);
Set<Character> chars = new HashSet<>();
// PART 1
for(int i = 0; i < input.length() - 4; i++) {
for(int j = 0; j < 4; j++) {
if(!chars.add(input.charAt(i + j))) {
break;
}
}
if(chars.size() == 4) {
System.out.println(i + 4);
break;
}
chars = new HashSet<>();
}
// PART 2
for(int i = 0; i < input.length() - 14; i++) {
for(int j = 0; j < 14; j++) {
if(!chars.add(input.charAt(i + j))) {
break;
}
}
if(chars.size() == 14) {
System.out.println(i + 14);
break;
}
chars = new HashSet<>();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}