Big update: Adding all advent of codes to same repo
This commit is contained in:
BIN
AdventOfCode2020/day13/day13.class
Normal file
BIN
AdventOfCode2020/day13/day13.class
Normal file
Binary file not shown.
50
AdventOfCode2020/day13/day13.java
Normal file
50
AdventOfCode2020/day13/day13.java
Normal file
@@ -0,0 +1,50 @@
|
||||
import java.util.Scanner;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
class day13 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
File myFile = new File("input.txt");
|
||||
try {
|
||||
|
||||
Scanner in = new Scanner(myFile);
|
||||
|
||||
double timestamp = Double.parseDouble(in.nextLine());
|
||||
String[] secondLine = in.nextLine().split(",");
|
||||
ArrayList<Double> buses = new ArrayList<>();
|
||||
|
||||
for (String i : secondLine) {
|
||||
if (!i.equals("x")) buses.add(Double.parseDouble(i));
|
||||
}
|
||||
|
||||
double shortestTime = Double.MAX_VALUE;
|
||||
double shortestBus = 0;
|
||||
|
||||
for (double i : buses) {
|
||||
|
||||
double closestTime = Math.ceil(timestamp / i) * i;
|
||||
System.out.printf("Time: %f | Closest Time: %f \n", i, closestTime);
|
||||
|
||||
if (closestTime < shortestTime) {
|
||||
shortestTime = closestTime;
|
||||
shortestBus = i;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
System.out.printf("Part 1: %f \n", (shortestTime - timestamp) * shortestBus);
|
||||
|
||||
in.close();
|
||||
|
||||
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user