|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +public class MohamedElderkaoui { |
| 4 | + |
| 5 | + public static void main(String[] args) { |
| 6 | + // Challenge 1: Batman Day calculation until 100th anniversary using Calendar |
| 7 | + int currentYear = 2024; |
| 8 | + int batmanAge = 85; |
| 9 | + System.out.println("Batman Day until 100th Anniversary:"); |
| 10 | + calculateBatmanDayUsingCalendar(currentYear, batmanAge); |
| 11 | + |
| 12 | + // Challenge 2: Batcave Security System |
| 13 | + System.out.println("\nBatcave Security System:"); |
| 14 | + // Sample sensor data: (x, y, threat level) |
| 15 | + List<int[]> sensors = List.of( |
| 16 | + new int[]{3, 3, 5}, |
| 17 | + new int[]{4, 4, 8}, |
| 18 | + new int[]{5, 5, 2}, |
| 19 | + new int[]{10, 10, 9}, |
| 20 | + new int[]{11, 11, 3}, |
| 21 | + new int[]{12, 12, 7} |
| 22 | + ); |
| 23 | + batcaveSecuritySystem(sensors); |
| 24 | + } |
| 25 | + |
| 26 | + // CHALLENGE 1: Calculate Batman Day until his 100th anniversary using Calendar |
| 27 | + public static void calculateBatmanDayUsingCalendar(int currentYear, int batmanAge) { |
| 28 | + int yearsLeft = 100 - batmanAge; |
| 29 | + for (int i = 0; i <= yearsLeft; i++) { |
| 30 | + int year = currentYear + i; |
| 31 | + Calendar thirdSaturday = getThirdSaturdayOfSeptember(year); |
| 32 | + System.out.printf("Batman Day in %d: %tF\n", year, thirdSaturday.getTime()); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // Get the third Saturday of September using Calendar |
| 37 | + private static Calendar getThirdSaturdayOfSeptember(int year) { |
| 38 | + // Create a calendar instance set to September 1st of the given year |
| 39 | + Calendar calendar = Calendar.getInstance(); |
| 40 | + calendar.set(year, Calendar.SEPTEMBER, 1); |
| 41 | + |
| 42 | + // Find the first Saturday of September |
| 43 | + while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) { |
| 44 | + calendar.add(Calendar.DAY_OF_MONTH, 1); |
| 45 | + } |
| 46 | + |
| 47 | + // Move to the third Saturday |
| 48 | + calendar.add(Calendar.DAY_OF_MONTH, 14); // Add 2 more weeks |
| 49 | + |
| 50 | + return calendar; |
| 51 | + } |
| 52 | + |
| 53 | + // CHALLENGE 2: Batcave Security System |
| 54 | + public static void batcaveSecuritySystem(List<int[]> sensors) { |
| 55 | + // 20x20 grid of Gotham |
| 56 | + int[][] gothamGrid = new int[20][20]; |
| 57 | + |
| 58 | + // Fill the grid with the threat levels from the sensor data |
| 59 | + for (int[] sensor : sensors) { |
| 60 | + int x = sensor[0]; |
| 61 | + int y = sensor[1]; |
| 62 | + int threatLevel = sensor[2]; |
| 63 | + gothamGrid[x][y] = threatLevel; |
| 64 | + } |
| 65 | + |
| 66 | + int maxThreatSum = 0; |
| 67 | + int centerX = 0, centerY = 0; |
| 68 | + |
| 69 | + // Find the 3x3 grid with the maximum threat level |
| 70 | + for (int i = 0; i <= 17; i++) { |
| 71 | + for (int j = 0; j <= 17; j++) { |
| 72 | + int threatSum = calculateThreatSum(gothamGrid, i, j); |
| 73 | + if (threatSum > maxThreatSum) { |
| 74 | + maxThreatSum = threatSum; |
| 75 | + centerX = i + 1; |
| 76 | + centerY = j + 1; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Calculate the distance from Batcave (0, 0) to the center of the most threatened grid |
| 82 | + int distanceToBatcave = Math.abs(centerX) + Math.abs(centerY); |
| 83 | + |
| 84 | + // Display the results |
| 85 | + System.out.println("Most threatened 3x3 grid center: (" + centerX + ", " + centerY + ")"); |
| 86 | + System.out.println("Sum of threats: " + maxThreatSum); |
| 87 | + System.out.println("Distance to Batcave: " + distanceToBatcave); |
| 88 | + |
| 89 | + // Check if the security protocol should be activated |
| 90 | + if (maxThreatSum > 20) { |
| 91 | + System.out.println("Security Protocol Activated!"); |
| 92 | + } else { |
| 93 | + System.out.println("Security Protocol NOT Activated."); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // Helper function to calculate the sum of threats in a 3x3 grid starting from (x, y) |
| 98 | + private static int calculateThreatSum(int[][] grid, int x, int y) { |
| 99 | + int sum = 0; |
| 100 | + for (int i = x; i < x + 3; i++) { |
| 101 | + for (int j = y; j < y + 3; j++) { |
| 102 | + sum += grid[i][j]; |
| 103 | + } |
| 104 | + } |
| 105 | + return sum; |
| 106 | + } |
| 107 | +} |
0 commit comments