Skip to content

Commit 68efed0

Browse files
author
MCqwertz
committed
+ day 6
1 parent 2f9e268 commit 68efed0

File tree

2 files changed

+2231
-0
lines changed

2 files changed

+2231
-0
lines changed

Day-06/java/MCqwertz/Day06.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//created by MCqwertz
2+
3+
package MCqwertz;
4+
5+
import java.io.FileNotFoundException;
6+
import java.util.*;
7+
8+
public class Day06 {
9+
public static void main(String[] args) throws FileNotFoundException {
10+
System.out.println("Part 1: " + getFirstResult());
11+
System.out.println("Part 1: " + getSecondResult());
12+
}
13+
14+
private static int getFirstResult() throws FileNotFoundException {
15+
TreeSet<Character> questions = new TreeSet<>();
16+
Scanner scanner = TextFileUtils.getScanner(6);
17+
int sum = 0;
18+
while(scanner.hasNextLine()) {
19+
char[] chars = scanner.nextLine().toCharArray();
20+
if(chars.length == 0) {
21+
sum += questions.size();
22+
questions.clear();
23+
} else {
24+
for (char c : chars) {
25+
questions.add(c);
26+
}
27+
}
28+
}
29+
sum += questions.size();
30+
return sum;
31+
}
32+
33+
private static int getSecondResult() throws FileNotFoundException {
34+
ArrayList<Character> questions = new ArrayList<>();
35+
Scanner scanner = TextFileUtils.getScanner(6);
36+
boolean firstLine = true;
37+
int sum = 0;
38+
while (scanner.hasNextLine()) {
39+
String nextLine = scanner.nextLine();
40+
if(nextLine.equals("")) {
41+
sum += questions.size();
42+
questions.clear();
43+
firstLine = true;
44+
} else {
45+
if(firstLine) {
46+
for (char c : nextLine.toCharArray()) {
47+
questions.add(c);
48+
}
49+
firstLine = false;
50+
} else {
51+
questions.removeIf(c -> !nextLine.contains(String.valueOf(c)));
52+
}
53+
}
54+
55+
}
56+
sum += questions.size();
57+
return sum;
58+
}
59+
}

0 commit comments

Comments
 (0)