Skip to content

Commit

Permalink
aoc 2017 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Dec 10, 2024
1 parent f3b616a commit 6fc379e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
26 changes: 12 additions & 14 deletions adventofcode/src/main/java/org/ck/adventofcode/year2017/Day02.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.ck.adventofcode.year2017;

import java.util.Arrays;
import java.util.LongSummaryStatistics;
import java.util.Scanner;
import java.util.*;
import java.util.function.ToLongFunction;
import org.ck.adventofcode.util.AOCSolution;
import org.ck.codechallengelib.annotation.Solution;
Expand All @@ -28,30 +26,30 @@ protected void runPartTwo(final Scanner in) {
run(in, Day02::getLineValueByDivision);
}

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

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

sum += getLineValue.applyAsLong(line);
}

print(sum);
}

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

return summary.getMax() - summary.getMin();
return line.getLast() - line.getFirst();
}

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

if ((first / second) * second == first) {
return first / second;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Day07 extends AOCSolution {

@Override
protected void runPartOne(final Scanner in) {
List<Program> programs = new ArrayList<>();
final List<Program> programs = new ArrayList<>();

while (in.hasNextLine()) {
final Matcher matcher = LINE_PATTERN.matcher(in.nextLine());
Expand All @@ -47,7 +47,7 @@ protected void runPartOne(final Scanner in) {
}
}

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

print(names.get(0));
Expand Down

0 comments on commit 6fc379e

Please sign in to comment.