-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.java
40 lines (36 loc) · 990 Bytes
/
Day13.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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
public class Day13 {
public static void main(String[] args) throws IOException {
List<String> lines = Files.readAllLines(Paths.get("input13.txt"));
int start = Integer.parseInt(lines.get(0));
String[] parts = lines.get(1).split(",");
List<Integer> buses = new LinkedList<>();
for (String p : parts)
if (!p.equals("x"))
buses.add(Integer.parseInt(p));
int min = Integer.MAX_VALUE;
int id = 0;
for (int bus : buses) {
int nextBus = ((start / bus) * bus) + bus - start;
if (nextBus < min) {
min = nextBus;
id = bus;
}
}
System.out.println(id * min);
long sum = 0, prod = 1;
for (int i = 0; i < parts.length; i++) {
if (parts[i].charAt(0) == 'x') continue;
int k = Integer.parseInt(parts[i]);
while (((sum + i) % k) != 0) {
sum += prod;
}
prod *= k;
}
System.out.println(sum);
}
}