|
| 1 | +# pylint: disable=missing-module-docstring,missing-function-docstring |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | +from typing import Literal, Tuple, TypedDict, List |
| 5 | + |
| 6 | + |
| 7 | +# ---------------------------------------------------------------------------- # |
| 8 | +# FIRST CHALLENGE # |
| 9 | +# ---------------------------------------------------------------------------- # |
| 10 | + |
| 11 | + |
| 12 | +def get_batman_day_anniversary(*, anniversary: int) -> datetime: |
| 13 | + anniversary_date: datetime = datetime(year=1939, month=9, day=16) |
| 14 | + |
| 15 | + if anniversary > 0: |
| 16 | + anniversary_date = datetime( |
| 17 | + year=anniversary_date.year + anniversary, |
| 18 | + month=anniversary_date.month, |
| 19 | + day=1, |
| 20 | + ) |
| 21 | + |
| 22 | + i: int = 1 |
| 23 | + saturday_counter: int = 1 if anniversary_date.weekday() == 5 else 0 |
| 24 | + |
| 25 | + while saturday_counter < 3: |
| 26 | + anniversary_date = datetime( |
| 27 | + year=anniversary_date.year, month=anniversary_date.month, day=i |
| 28 | + ) |
| 29 | + |
| 30 | + if anniversary_date.weekday() == 5: |
| 31 | + saturday_counter += 1 |
| 32 | + |
| 33 | + i += 1 |
| 34 | + |
| 35 | + return anniversary_date |
| 36 | + |
| 37 | + |
| 38 | +batman_day_85th_anniversary: datetime = get_batman_day_anniversary(anniversary=85) |
| 39 | +batman_day_100th_anniversary: datetime = get_batman_day_anniversary(anniversary=100) |
| 40 | + |
| 41 | +print("> First challenge...") |
| 42 | + |
| 43 | +print( |
| 44 | + f"\n> The 85th anniversary of Batman day is on " |
| 45 | + f"{batman_day_85th_anniversary.month}/" |
| 46 | + f"{batman_day_85th_anniversary.day}/" |
| 47 | + f"{batman_day_85th_anniversary.year}." |
| 48 | +) |
| 49 | + |
| 50 | +print( |
| 51 | + f"\n> The 100th anniversary of Batman day is on " |
| 52 | + f"{batman_day_100th_anniversary.month}/" |
| 53 | + f"{batman_day_100th_anniversary.day}/" |
| 54 | + f"{batman_day_100th_anniversary.year}." |
| 55 | +) |
| 56 | + |
| 57 | + |
| 58 | +# ---------------------------------------------------------------------------- # |
| 59 | +# SECOND CHALLENGE # |
| 60 | +# ---------------------------------------------------------------------------- # |
| 61 | + |
| 62 | + |
| 63 | +# ----------------------------------- Types ---------------------------------- # |
| 64 | + |
| 65 | + |
| 66 | +type ThreatLevel = Literal[ |
| 67 | + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 |
| 68 | +] |
| 69 | + |
| 70 | +type RowSensors = Tuple[ |
| 71 | + ThreatLevel, |
| 72 | + ThreatLevel, |
| 73 | + ThreatLevel, |
| 74 | + ThreatLevel, |
| 75 | + ThreatLevel, |
| 76 | + ThreatLevel, |
| 77 | + ThreatLevel, |
| 78 | + ThreatLevel, |
| 79 | + ThreatLevel, |
| 80 | + ThreatLevel, |
| 81 | + ThreatLevel, |
| 82 | + ThreatLevel, |
| 83 | + ThreatLevel, |
| 84 | + ThreatLevel, |
| 85 | + ThreatLevel, |
| 86 | + ThreatLevel, |
| 87 | + ThreatLevel, |
| 88 | + ThreatLevel, |
| 89 | + ThreatLevel, |
| 90 | + ThreatLevel, |
| 91 | +] |
| 92 | + |
| 93 | +type Sensors = Tuple[ |
| 94 | + RowSensors, |
| 95 | + RowSensors, |
| 96 | + RowSensors, |
| 97 | + RowSensors, |
| 98 | + RowSensors, |
| 99 | + RowSensors, |
| 100 | + RowSensors, |
| 101 | + RowSensors, |
| 102 | + RowSensors, |
| 103 | + RowSensors, |
| 104 | + RowSensors, |
| 105 | + RowSensors, |
| 106 | + RowSensors, |
| 107 | + RowSensors, |
| 108 | + RowSensors, |
| 109 | + RowSensors, |
| 110 | + RowSensors, |
| 111 | + RowSensors, |
| 112 | + RowSensors, |
| 113 | + RowSensors, |
| 114 | +] |
| 115 | + |
| 116 | +ThreatLevelPos = TypedDict("ThreatLevelPos", {"x": int, "y": int, "threat_level": int}) |
| 117 | + |
| 118 | + |
| 119 | +# ----------------------------------- Main ----------------------------------- # |
| 120 | + |
| 121 | + |
| 122 | +def get_threat_levels(*, _sensors: Sensors) -> List[ThreatLevelPos]: |
| 123 | + _threat_levels: List[ThreatLevelPos] = [] |
| 124 | + |
| 125 | + for i in range(1, len(_sensors) - 1): |
| 126 | + for j in range(1, len(_sensors[i]) - 1): |
| 127 | + _threat_level: int = 0 |
| 128 | + |
| 129 | + _threat_level += _sensors[i - 1][j - 1] |
| 130 | + _threat_level += _sensors[i - 1][j] |
| 131 | + _threat_level += _sensors[i - 1][j + 1] |
| 132 | + |
| 133 | + _threat_level += _sensors[i][j - 1] |
| 134 | + _threat_level += _sensors[i][j] |
| 135 | + _threat_level += _sensors[i][j + 1] |
| 136 | + |
| 137 | + _threat_level += _sensors[i + 1][j - 1] |
| 138 | + _threat_level += _sensors[i + 1][j] |
| 139 | + _threat_level += _sensors[i + 1][j + 1] |
| 140 | + |
| 141 | + _threat_levels.append({"x": j, "threat_level": _threat_level, "y": i}) |
| 142 | + |
| 143 | + return _threat_levels |
| 144 | + |
| 145 | + |
| 146 | +batman_cave_pos: Tuple[int, int] = (0, 0) |
| 147 | + |
| 148 | +sensors: Sensors = ( |
| 149 | + (1, 0, 1, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1), |
| 150 | + (2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0), |
| 151 | + (1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1), |
| 152 | + (0, 9, 8, 0, 2, 0, 9, 2, 8, 1, 0, 1, 3, 7, 8, 1, 0, 2, 7, 6), |
| 153 | + (1, 0, 1, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1), |
| 154 | + (3, 1, 8, 7, 9, 6, 1, 7, 9, 0, 1, 0, 1, 9, 2, 1, 3, 0, 8, 0), |
| 155 | + (1, 6, 9, 1, 2, 8, 0, 2, 1, 3, 2, 8, 7, 2, 8, 0, 1, 7, 9, 0), |
| 156 | + (1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 1, 0), |
| 157 | + (0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0), |
| 158 | + (2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2), |
| 159 | + (7, 8, 6, 1, 0, 9, 2, 8, 1, 3, 7, 1, 0, 2, 0, 9, 1, 6, 7, 8), |
| 160 | + (0, 1, 7, 9, 2, 8, 1, 6, 7, 8, 1, 2, 0, 3, 1, 8, 2, 7, 0, 1), |
| 161 | + (3, 0, 9, 1, 8, 6, 1, 0, 7, 9, 2, 0, 1, 2, 8, 0, 1, 9, 6, 1), |
| 162 | + (1, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0), |
| 163 | + (0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0), |
| 164 | + (2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2), |
| 165 | + (9, 6, 1, 0, 9, 8, 7, 2, 8, 0, 1, 0, 3, 6, 2, 7, 9, 2, 8, 1), |
| 166 | + (0, 7, 8, 9, 6, 2, 1, 9, 6, 7, 8, 0, 9, 7, 6, 8, 1, 0, 2, 8), |
| 167 | + (2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2), |
| 168 | + (3, 2, 1, 9, 6, 7, 0, 3, 1, 8, 2, 7, 9, 2, 0, 9, 7, 6, 8, 0), |
| 169 | +) |
| 170 | + |
| 171 | +threat_levels: List[ThreatLevelPos] = get_threat_levels(_sensors=sensors) |
| 172 | + |
| 173 | +threat_levels_greater_than_twenty = list( |
| 174 | + filter(lambda threat_level: threat_level["threat_level"] >= 20, threat_levels) |
| 175 | +) |
| 176 | + |
| 177 | +print("\n> Second challenge...") |
| 178 | + |
| 179 | +print("\n> Threat levels greater than 20 (security protocol activated)...") |
| 180 | + |
| 181 | +for threat_level in threat_levels_greater_than_twenty: |
| 182 | + print(f"\n> Coordinates (x, y): ({threat_level['x']}, {threat_level['y']}).") |
| 183 | + print(f"> Threat level: {threat_level['threat_level']}.") |
| 184 | + |
| 185 | +print("\n> Position with the maximum threat level...") |
| 186 | + |
| 187 | +max_threat_level: ThreatLevelPos = max( |
| 188 | + iter(threat_levels_greater_than_twenty), |
| 189 | + key=lambda threat_level: threat_level["threat_level"], |
| 190 | +) |
| 191 | + |
| 192 | +distance_to_batman_cave: int = abs(max_threat_level["x"] - batman_cave_pos[0]) + abs( |
| 193 | + max_threat_level["y"] - batman_cave_pos[1] |
| 194 | +) |
| 195 | + |
| 196 | +print(f"\n> Coordinates (x, y): ({max_threat_level['x']}, {max_threat_level['y']}).") |
| 197 | +print(f"> Threat level: {max_threat_level['threat_level']}.") |
| 198 | +print(f"> Distance to batman cave: {distance_to_batman_cave} cells.") |
0 commit comments