-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle1.java
66 lines (58 loc) · 1.76 KB
/
Puzzle1.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
61
62
63
64
65
66
package advent2022;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.CharStreams;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
/**
* @author Éamonn McManus
*/
public class Puzzle1 {
private static final String SAMPLE =
"""
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
""";
private static final Map<String, Callable<Reader>> INPUT_PRODUCERS =
ImmutableMap.of(
"sample", () -> new StringReader(SAMPLE),
"problem", () -> new InputStreamReader(Puzzle1.class.getResourceAsStream("puzzle1.txt")));
public static void main(String[] args) throws Exception {
for (var entry : INPUT_PRODUCERS.entrySet()) {
String name = entry.getKey();
try (Reader r = entry.getValue().call()) {
List<String> lines = CharStreams.readLines(r);
List<List<String>> groups = new ArrayList<>();
while (true) {
int index = lines.indexOf("");
if (index < 0) {
break;
}
groups.add(lines.subList(0, index));
lines = lines.subList(index + 1, lines.size());
}
groups.add(lines);
List<Long> totals =
groups.stream()
.map(group -> group.stream().mapToLong(Long::parseLong).sum())
.sorted()
.toList();
System.out.println("Max for " + name + " is " + totals.getLast());
long topThree = totals.stream().skip(totals.size() - 3).mapToLong(Long::valueOf).sum();
System.out.println("Sum of top three is " + topThree);
}
}
}
}