-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.java
52 lines (44 loc) · 1.45 KB
/
Day1.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
package aoc2018.day01;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
public class Day1 {
public static int frequencyCalculator(String s) {
return IntStream.of(Arrays.stream(s.split(", ")).mapToInt(Integer::parseInt).toArray()).sum();
}
public static int findFirstRepeatedFrequency(String s) {
int sum = 0;
List<Integer> frequencies = new ArrayList<>();
frequencies.add(sum);
int[] values = Arrays.stream(s.split(", ")).mapToInt(Integer::parseInt).toArray();
while (true)
for (int val : values) {
sum += val;
if (frequencies.contains(sum)) {
return sum;
} else {
frequencies.add(sum);
}
}
}
public static String readInput(String inputRulesFile) {
String formattedInput = "";
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(inputRulesFile));
String line = reader.readLine();
while (line != null) {
formattedInput += line + ", ";
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return formattedInput;
}
}