2020aoc/Day10.java

76 lines
2.5 KiB
Java

package dev.forstenlechner.aoc;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
public class Day10 {
public static void main(String[] args) throws Exception {
new Day10().solve();
}
public record Jolts (int previous, int countOneJolts, int countTwoJolts,int countThreeJolts) {};
private long calcCombinations(List<Integer> joltJunk) {
return switch (joltJunk.size()) {
case 3 -> 2;
case 4 -> 4;
case 5 -> 7;
default -> 1;
};
}
public void solve() throws Exception {
System.out.println("Solve");
Stream<String> lines = Files.lines(Paths.get("day10/input"));
List<Integer> sortedJolts = Stream.concat(Stream.of(0), lines.map(Integer::parseInt).sorted()).collect(toList());
Jolts jolts = sortedJolts.stream().reduce(new Jolts(0, 0, 0, 1),
(jolt, next) ->
new Jolts(next, jolt.countOneJolts() + (next - jolt.previous() == 1 ? 1 : 0), jolt.countTwoJolts() + (next - jolt.previous() == 2 ? 1 : 0), jolt.countThreeJolts() + (next - jolt.previous() == 3 ? 1 : 0))
, (j1, j2) -> j1);
System.out.println(jolts.countOneJolts * jolts.countThreeJolts);
// 1690
System.out.println(jolts.countOneJolts);
System.out.println(jolts.countTwoJolts);
System.out.println(jolts.countThreeJolts);
List<List<Integer>> junks = new ArrayList<>();
List<Integer> currentJunk = new ArrayList<>();
int expected = 0;
for (Integer jolt : sortedJolts) {
if (expected == jolt) {
currentJunk.add(jolt);
expected++;
continue;
}
if (currentJunk.isEmpty()) {
currentJunk.add(jolt);
expected = jolt + 1;
continue;
}
junks.add(currentJunk);
currentJunk = new ArrayList<>();
currentJunk.add(jolt);
expected = jolt + 1;
}
if (!currentJunk.isEmpty()) {
junks.add(currentJunk);
}
System.out.println(junks);
Long reduce = junks.stream().map(this::calcCombinations).reduce(1L, (a, b) -> a * b);
System.out.println(reduce);
}
}