-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC062023.java
41 lines (30 loc) · 1.19 KB
/
AoC062023.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
package com.adventofcode.aoc2023;
import static com.adventofcode.utils.Utils.itoa;
import com.adventofcode.Solution;
import com.adventofcode.utils.Utils;
import com.google.common.collect.Streams;
import java.util.List;
import java.util.stream.LongStream;
import java.util.stream.Stream;
class AoC062023 implements Solution {
@Override
public String solveFirstPart(final Stream<String> input) {
return solve( input.toList() );
}
@Override
public String solveSecondPart(final Stream<String> input) {
return solve( input.map( s -> s.replace( " ", "" ) ).toList() );
}
private String solve(final List<String> input) {
final Stream<Long> times = Utils.toLongStream( input.get( 0 ) );
final Stream<Long> distances = Utils.toLongStream( input.get( 1 ) );
final Stream<Race> races = Streams.zip( times, distances, Race::new );
return itoa( races.mapToLong( AoC062023::countVictories ).reduce( 1, (a, b) -> a * b ) );
}
private static long countVictories(final Race race) {
return LongStream.range( 1, race.time ).parallel().map( n -> n * (race.time - n) )
.filter( n -> n > race.distance ).count();
}
private record Race(long time, long distance) {
}
}