|
| 1 | +from datetime import datetime, timedelta |
| 2 | + |
| 3 | +# Reto 1 |
| 4 | + |
| 5 | +year_of_creation = 1939 |
| 6 | +anniversary_year = year_of_creation + 85 |
| 7 | + |
| 8 | +batman_day_anniversary_dates = [] |
| 9 | + |
| 10 | +while anniversary_year <= year_of_creation + 100: |
| 11 | + |
| 12 | + september = datetime(anniversary_year, 9, 1) |
| 13 | + |
| 14 | + first_saturday = 5 - september.weekday() if september.weekday() <= 5 else 12 - \ |
| 15 | + september.weekday() |
| 16 | + |
| 17 | + third_saturday = september + timedelta(days=first_saturday + 14) |
| 18 | + |
| 19 | + batman_day_anniversary_dates.append( |
| 20 | + ( |
| 21 | + anniversary_year, |
| 22 | + anniversary_year - year_of_creation, |
| 23 | + third_saturday.strftime("%d-%m-%Y") |
| 24 | + ) |
| 25 | + ) |
| 26 | + |
| 27 | + anniversary_year += 1 |
| 28 | + |
| 29 | +for year, anniversary, batman_day in batman_day_anniversary_dates: |
| 30 | + print(f"Batman day {year} ({anniversary} aniversario): {batman_day}") |
| 31 | + |
| 32 | +# Reto 2 |
| 33 | + |
| 34 | + |
| 35 | +def sum_subgrid_alerts(sensors, center_x, center_y) -> int: |
| 36 | + |
| 37 | + total = 0 |
| 38 | + |
| 39 | + for x in range(center_x - 1, center_x + 2): |
| 40 | + for y in range(center_y - 1, center_y + 2): |
| 41 | + for sensor in sensors: |
| 42 | + if sensor[0] == x and sensor[1] == y: |
| 43 | + total += sensor[2] |
| 44 | + |
| 45 | + return total |
| 46 | + |
| 47 | + |
| 48 | +def batcave_security_system(sensors): |
| 49 | + |
| 50 | + max_alert_level = 0 |
| 51 | + max_alert_coordinate = (0, 0) |
| 52 | + |
| 53 | + for x in range(1, 19): |
| 54 | + for y in range(1, 19): |
| 55 | + alert_level = sum_subgrid_alerts(sensors, x, y) |
| 56 | + if alert_level > max_alert_level: |
| 57 | + max_alert_level = alert_level |
| 58 | + max_alert_coordinate = (x, y) |
| 59 | + |
| 60 | + distance = abs(max_alert_coordinate[0]) + abs(max_alert_coordinate[1]) |
| 61 | + activate_protocol = max_alert_level > 20 |
| 62 | + |
| 63 | + return max_alert_coordinate, max_alert_level, distance, activate_protocol |
| 64 | + |
| 65 | + |
| 66 | +sensors = [ |
| 67 | + (2, 3, 7), |
| 68 | + (4, 3, 8), |
| 69 | + (2, 2, 7), |
| 70 | + (10, 12, 8), |
| 71 | + (11, 11, 8), |
| 72 | + (10, 11, 8), |
| 73 | + (15, 18, 4) |
| 74 | +] |
| 75 | + |
| 76 | +result = batcave_security_system(sensors) |
| 77 | +print(f"Centro cuadrícula más amenazada: {result[0]}.") |
| 78 | +print(f"Máximo nivel de alerta: {result[1]}.") |
| 79 | +print(f"Distancia a la Batcueva: {result[2]}.") |
| 80 | +print(f"Activar protocolo de seguridad: {"Sí" if result[3] else "No"}.") |
0 commit comments