Skip to content

Commit 6fc379e

Browse files
committed
aoc 2017 refactor
1 parent f3b616a commit 6fc379e

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

adventofcode/src/main/java/org/ck/adventofcode/year2017/Day02.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.ck.adventofcode.year2017;
22

3-
import java.util.Arrays;
4-
import java.util.LongSummaryStatistics;
5-
import java.util.Scanner;
3+
import java.util.*;
64
import java.util.function.ToLongFunction;
75
import org.ck.adventofcode.util.AOCSolution;
86
import org.ck.codechallengelib.annotation.Solution;
@@ -28,30 +26,30 @@ protected void runPartTwo(final Scanner in) {
2826
run(in, Day02::getLineValueByDivision);
2927
}
3028

31-
private void run(final Scanner in, final ToLongFunction<String[]> getLineValue) {
29+
private void run(final Scanner in, final ToLongFunction<List<Long>> getLineValue) {
3230
long sum = 0;
3331

3432
while (in.hasNextLine()) {
35-
final String[] line = in.nextLine().split("\\s");
33+
final List<Long> line =
34+
new ArrayList<>(Arrays.stream(in.nextLine().split("\\s")).map(Long::valueOf).toList());
3635

3736
sum += getLineValue.applyAsLong(line);
3837
}
3938

4039
print(sum);
4140
}
4241

43-
private static long getLineValueByDifference(final String[] line) {
44-
final LongSummaryStatistics summary =
45-
Arrays.stream(line).mapToLong(Long::parseLong).summaryStatistics();
42+
private static long getLineValueByDifference(final List<Long> line) {
43+
Collections.sort(line);
4644

47-
return summary.getMax() - summary.getMin();
45+
return line.getLast() - line.getFirst();
4846
}
4947

50-
private static long getLineValueByDivision(final String[] line) {
51-
for (int i = 0; i < line.length - 1; ++i) {
52-
for (int j = i + 1; j < line.length; ++j) {
53-
final int first = Integer.parseInt(line[i]);
54-
final int second = Integer.parseInt(line[j]);
48+
private static long getLineValueByDivision(final List<Long> line) {
49+
for (int i = 0; i < line.size() - 1; ++i) {
50+
for (int j = i + 1; j < line.size(); ++j) {
51+
final long first = line.get(i);
52+
final long second = line.get(j);
5553

5654
if ((first / second) * second == first) {
5755
return first / second;

adventofcode/src/main/java/org/ck/adventofcode/year2017/Day07.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Day07 extends AOCSolution {
2727

2828
@Override
2929
protected void runPartOne(final Scanner in) {
30-
List<Program> programs = new ArrayList<>();
30+
final List<Program> programs = new ArrayList<>();
3131

3232
while (in.hasNextLine()) {
3333
final Matcher matcher = LINE_PATTERN.matcher(in.nextLine());
@@ -47,7 +47,7 @@ protected void runPartOne(final Scanner in) {
4747
}
4848
}
4949

50-
List<String> names = programs.stream().map(Program::name).collect(Collectors.toList());
50+
final List<String> names = programs.stream().map(Program::name).collect(Collectors.toList());
5151
programs.forEach(program -> Arrays.stream(program.above()).forEach(names::remove));
5252

5353
print(names.get(0));

0 commit comments

Comments
 (0)