-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShuttleSearch.java
147 lines (121 loc) · 5.24 KB
/
ShuttleSearch.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Your ferry can make it safely to a nearby port, but it won't get much further. When you call to book another ship, you discover that no ships embark from that port to your vacation island. You'll need to get from the port to the nearest airport.
Fortunately, a shuttle bus service is available to bring you from the sea port to the airport! Each bus has an ID number that also indicates how often the bus leaves for the airport.
Bus schedules are defined based on a timestamp that measures the number of minutes since some fixed reference point in the past. At timestamp 0, every bus simultaneously departed from the sea port. After that, each bus travels to the airport, then various other locations, and finally returns to the sea port to repeat its journey forever.
The time this loop takes a particular bus is also its ID number: the bus with ID 5 departs from the sea port at timestamps 0, 5, 10, 15, and so on. The bus with ID 11 departs at 0, 11, 22, 33, and so on. If you are there when the bus departs, you can ride that bus to the airport!
Your notes (your puzzle input) consist of two lines. The first line is your estimate of the earliest timestamp you could depart on a bus. The second line lists the bus IDs that are in service according to the shuttle company; entries that show x must be out of service, so you decide to ignore them.
To save time once you arrive, your goal is to figure out the earliest bus you can take to the airport. (There will be exactly one such bus.)
For example, suppose you have the following notes:
939
7,13,x,x,59,x,31,19
Here, the earliest timestamp you could depart is 939, and the bus IDs in service are 7, 13, 59, 31, and 19. Near timestamp 939, these bus IDs depart at the times marked D:
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class ShuttleSearch {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("input13.txt");
Scanner myReader = new Scanner(file);
int stamp = myReader.nextInt();
myReader.nextLine();
String[] idBus = myReader.nextLine().split(",");
System.out.println(part2another(stamp,idBus));
}
private static int part1(int stamp, String[] idBus) {
int selectID = 0;
int diff = Integer.MAX_VALUE;
for (int i =0; i < idBus.length; i++)
{
String item = idBus[i];
if (!item.equals("x") && (Integer.parseInt(item)-(stamp % Integer.parseInt(item))) < diff)
{
diff = (Integer.parseInt(item)-(stamp % Integer.parseInt(item)));
selectID = i;
}
}
return Integer.parseInt(idBus[selectID])*diff;
}
private static long part2(int stamp, String[] idBus) {
long result = 0;
long lcm = 1;
int[] items = new int[idBus.length];
for (int i = 0; i < idBus.length;i++)
{
String item = idBus[i];
if (!item.equals("x")) {
items[i] = Integer.parseInt(item);
}else
{
items[i] = -1;
}
}
for (int i = 0; i< items.length;i++)
{
if (items[i] != -1 )
{
long item = items[i];
if (i!=0)
while ((result % item) != (item-i) )
{
result += lcm;
}
System.out.println(lcm);
lcm = lcm(lcm,item);
System.out.println(lcm);
System.out.println("======");
}
}
return result%lcm;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
if (a>b)
return gcd(b, a % b);
return gcd(a, b % a);
}
static long lcm(long a, long b)
{
return (a*b)/gcd(a,b);
}
private static String part2another(int stamp, String[] idBus) {
BigInteger result = new BigInteger("0");
long M = 1;
int[] items = new int[idBus.length];
for (int i = 0; i < idBus.length;i++)
{
String item = idBus[i];
if (!item.equals("x")) {
items[i] = Integer.parseInt(item);
M*=items[i];
}else
{
items[i] = -1;
}
}
for (int i = 0; i< items.length;i++)
{
if (items[i] != -1) {
long m = M/items[i];
long y = m % items[i];
int j = 1;
while ((y*j)%items[i] != 1)
j++;
y = j % items[i];
int a = i;
if (i != 0)
a = ((items[i]-i));
BigInteger add = new BigInteger(String.valueOf(a));
add.multiply(BigInteger.valueOf(y));
add.multiply(BigInteger.valueOf(m));
result = new BigInteger(String.valueOf(new BigInteger(String.valueOf(a)).multiply(BigInteger.valueOf(y)).multiply(BigInteger.valueOf(m)))).add(result);
System.out.println(result);
}
}
return result.mod(BigInteger.valueOf(M)).toString();
}
}