Denis2312

OffroadChallenge

Oct 21st, 2023
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class OffRoadAdventure {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         ArrayList<Integer> initialFuel = parseInput(scanner.nextLine());
  9.         ArrayList<Integer> consumptionIndex = parseInput(scanner.nextLine());
  10.         ArrayList<Integer> neededFuelSequence = parseInput(scanner.nextLine());
  11.  
  12.         ArrayList<Integer> reachedAltitudes = new ArrayList<>();
  13.         int currentAltitude = 1;
  14.  
  15.         while (!neededFuelSequence.isEmpty()) {
  16.             if (initialFuel.isEmpty() || consumptionIndex.isEmpty()) {
  17.                 break;
  18.             }
  19.  
  20.             int currentFuel = initialFuel.get(initialFuel.size() - 1);
  21.             int currentConsumption = consumptionIndex.get(0);
  22.             int neededFuel = neededFuelSequence.get(0);
  23.  
  24.             if (canReachAltitude(currentFuel, currentConsumption, neededFuel)) {
  25.                 reachedAltitudes.add(currentAltitude);
  26.                 System.out.printf("John has reached: Altitude %d%n", currentAltitude);
  27.                 initialFuel.remove(initialFuel.size() - 1);
  28.                 consumptionIndex.remove(0);
  29.                 neededFuelSequence.remove(0);
  30.             } else {
  31.                 System.out.printf("John did not reach: Altitude %d%n", currentAltitude);
  32.                 break;
  33.             }
  34.             currentAltitude++;
  35.         }
  36.  
  37.         if (reachedAltitudes.isEmpty()) {
  38.             System.out.println("John failed to reach the top.");
  39.             System.out.println("John didn't reach any altitude.");
  40.         } else {
  41.             if (neededFuelSequence.isEmpty()) {
  42.                 System.out.println("John has reached all the altitudes and managed to reach the top!");
  43.             } else {
  44.                 System.out.printf("John failed to reach the top.%n");
  45.                 System.out.print("Reached altitudes: ");
  46.                 int numberOfAltitudes = reachedAltitudes.size();
  47.                 for (int i = 0; i < numberOfAltitudes; i++) {
  48.                     if (i < numberOfAltitudes - 1) {
  49.                         System.out.printf("Altitude %d, ", reachedAltitudes.get(i));
  50.                     } else {
  51.                         System.out.printf("Altitude %d", reachedAltitudes.get(i));
  52.                     }
  53.                 }
  54.                 System.out.println();
  55.             }
  56.         }
  57.     }
  58.  
  59.     private static ArrayList<Integer> parseInput(String input) {
  60.         String[] parts = input.split(" ");
  61.         ArrayList<Integer> list = new ArrayList<>();
  62.         for (String part : parts) {
  63.             list.add(Integer.parseInt(part));
  64.         }
  65.         return list;
  66.     }
  67.  
  68.     private static boolean canReachAltitude(int fuel, int consumption, int neededFuel) {
  69.         return (fuel - consumption) >= neededFuel;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment