Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class OffRoadAdventure {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- ArrayList<Integer> initialFuel = parseInput(scanner.nextLine());
- ArrayList<Integer> consumptionIndex = parseInput(scanner.nextLine());
- ArrayList<Integer> neededFuelSequence = parseInput(scanner.nextLine());
- ArrayList<Integer> reachedAltitudes = new ArrayList<>();
- int currentAltitude = 1;
- while (!neededFuelSequence.isEmpty()) {
- if (initialFuel.isEmpty() || consumptionIndex.isEmpty()) {
- break;
- }
- int currentFuel = initialFuel.get(initialFuel.size() - 1);
- int currentConsumption = consumptionIndex.get(0);
- int neededFuel = neededFuelSequence.get(0);
- if (canReachAltitude(currentFuel, currentConsumption, neededFuel)) {
- reachedAltitudes.add(currentAltitude);
- System.out.printf("John has reached: Altitude %d%n", currentAltitude);
- initialFuel.remove(initialFuel.size() - 1);
- consumptionIndex.remove(0);
- neededFuelSequence.remove(0);
- } else {
- System.out.printf("John did not reach: Altitude %d%n", currentAltitude);
- break;
- }
- currentAltitude++;
- }
- if (reachedAltitudes.isEmpty()) {
- System.out.println("John failed to reach the top.");
- System.out.println("John didn't reach any altitude.");
- } else {
- if (neededFuelSequence.isEmpty()) {
- System.out.println("John has reached all the altitudes and managed to reach the top!");
- } else {
- System.out.printf("John failed to reach the top.%n");
- System.out.print("Reached altitudes: ");
- int numberOfAltitudes = reachedAltitudes.size();
- for (int i = 0; i < numberOfAltitudes; i++) {
- if (i < numberOfAltitudes - 1) {
- System.out.printf("Altitude %d, ", reachedAltitudes.get(i));
- } else {
- System.out.printf("Altitude %d", reachedAltitudes.get(i));
- }
- }
- System.out.println();
- }
- }
- }
- private static ArrayList<Integer> parseInput(String input) {
- String[] parts = input.split(" ");
- ArrayList<Integer> list = new ArrayList<>();
- for (String part : parts) {
- list.add(Integer.parseInt(part));
- }
- return list;
- }
- private static boolean canReachAltitude(int fuel, int consumption, int neededFuel) {
- return (fuel - consumption) >= neededFuel;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment