-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13Part1.java
36 lines (29 loc) · 1.11 KB
/
Day13Part1.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
package uk.oczadly.karl.aoc20.solution.day13;
import uk.oczadly.karl.aoc20.input.NoValidSolutionException;
import uk.oczadly.karl.aoc20.PuzzleSolution;
import uk.oczadly.karl.aoc20.input.PuzzleInput;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Karl Oczadly
*/
public class Day13Part1 extends PuzzleSolution {
public Day13Part1() { super(13, 1); } // Initializes the day and part number
@Override
public Object solve(PuzzleInput input) {
// Load input
int earliestTimestamp = Integer.parseInt(input.nextLine());
List<Integer> busIds = Arrays.stream(input.nextLine().split(","))
.filter(s -> !s.equals("x"))
.map(Integer::parseInt)
.collect(Collectors.toList());
// Process
for (int ts = earliestTimestamp; ts < Integer.MAX_VALUE; ts++) {
for (int busSchedule : busIds)
if (ts % busSchedule == 0)
return busSchedule * (ts - earliestTimestamp);
}
throw new NoValidSolutionException();
}
}