-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay6.java
46 lines (35 loc) · 1.21 KB
/
Day6.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
package com.sbaars.adventofcode.year23.days;
import com.sbaars.adventofcode.year23.Day2023;
import java.util.List;
import static com.sbaars.adventofcode.util.DataMapper.readString;
import static java.util.stream.IntStream.range;
import static java.util.stream.IntStream.rangeClosed;
public class Day6 extends Day2023 {
public Day6() {
super(6);
}
public static void main(String[] args) {
new Day6().printParts();
}
public record Results(List<Integer> time, List<Integer> distance) {
}
@Override
public Object part1() {
Results in = readString(day().replaceAll(" +", " "), "Time: %li\nDistance: %li", " ", Results.class);
return range(0, in.time.size())
.mapToLong(i -> countWays(in.time.get(i), in.distance.get(i)))
.reduce(1, (a, b) -> a * b);
}
private static long countWays(int time, long distance) {
return rangeClosed(0, time)
.filter(holdTime -> (long) holdTime * (time - holdTime) > distance)
.count();
}
public record SingleRace(int time, long distance) {
}
@Override
public Object part2() {
SingleRace in = readString(day().replaceAll(" ", ""), "Time:%i\nDistance:%n", SingleRace.class);
return countWays(in.time, in.distance);
}
}