|
| 1 | +import java.time.DayOfWeek; |
| 2 | +import java.time.LocalDate; |
| 3 | +import java.time.temporal.TemporalAdjusters; |
| 4 | +import java.util.LinkedList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Random; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + |
| 10 | + public static void main(String[] args) { |
| 11 | + calculateBatmanDay(); |
| 12 | + batcaveSecuritySystem(); |
| 13 | + } |
| 14 | + |
| 15 | + /* |
| 16 | + RETO 1: |
| 17 | + Crea un programa que calcule cuándo se va a celebrar el Batman Day hasta su 100 aniversario. |
| 18 | + El Batman Day se celebra el tercer sábado de septiembre de cada año. |
| 19 | + */ |
| 20 | + public static void calculateBatmanDay() { |
| 21 | + List<LocalDate> batmanDayAnniversary = new LinkedList<>(); |
| 22 | + int year = 1939; |
| 23 | + |
| 24 | + for (int i = 0; i < 100; i++) { |
| 25 | + LocalDate firstDayOfSeptember = LocalDate.of(year, 9, 1); |
| 26 | + LocalDate firstSaturday = firstDayOfSeptember.with(TemporalAdjusters.firstInMonth(DayOfWeek.SATURDAY)); |
| 27 | + LocalDate thirdSaturday = firstSaturday.plusWeeks(2); |
| 28 | + batmanDayAnniversary.add(thirdSaturday); |
| 29 | + year++; |
| 30 | + } |
| 31 | + |
| 32 | + batmanDayAnniversary.forEach(day -> System.out.println("El sábado de la tercera semana de septiembre de " + day.getYear() + " es el día " + day.getDayOfMonth())); |
| 33 | + } |
| 34 | + |
| 35 | + /* |
| 36 | + Reto 2: |
| 37 | + */ |
| 38 | + public static void batcaveSecuritySystem() { |
| 39 | + Sensor[][] grid = new Sensor[20][20]; |
| 40 | + Random random = new Random(); |
| 41 | + |
| 42 | + for (int x = 0; x < 20; x++) { |
| 43 | + for (int y = 0; y < 20; y++) { |
| 44 | + grid[x][y] = new Sensor(x, y, random.nextInt(11)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + int centerX = 0, centerY = 0, maxSum = 0; |
| 49 | + |
| 50 | + for (int x = 0; x <= 17; x++) { |
| 51 | + for (int y = 0; y <= 17; y++) { |
| 52 | + int currSum = calculateSum(grid, x, y); |
| 53 | + if (currSum > maxSum) { |
| 54 | + maxSum = currSum; |
| 55 | + centerX = x + 1; |
| 56 | + centerY = y + 1; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + int distanceToBatcave = Math.abs(centerX) + Math.abs(centerY); |
| 62 | + |
| 63 | + System.out.println("Most intense area is at (" + centerX + ", " + centerY + ") with a sum of " + maxSum); |
| 64 | + System.out.println("Distance to Batcave: " + distanceToBatcave); |
| 65 | + System.out.println("Security protocol should be activated? " + ((maxSum > 20) ? "YES" : "NO")); |
| 66 | + } |
| 67 | + |
| 68 | + private static int calculateSum(Sensor[][] grid, int x, int y) { |
| 69 | + int sum = 0; |
| 70 | + for (int i = x; i < x + 3; i++) { |
| 71 | + for (int j = y; j < y + 3; j++) { |
| 72 | + sum += grid[i][j].level; |
| 73 | + } |
| 74 | + } |
| 75 | + return sum; |
| 76 | + } |
| 77 | + |
| 78 | + record Sensor(int x, int y, int level) {} |
| 79 | +} |
0 commit comments